aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

System Environment Variables not being read on server

Open Allann opened this issue 9 years ago • 9 comments

I have an asp.net core website which works fine locally. I use environment variables to store the Environment and a couple of connection strings. Not a problem locally. When i deploy the website to the server (via release manager, xcopy and powershell scripts running as an administrator) the appropriate variables are created/updated on that server. I have confirmed that they are there.

I start the website and I get the following returned in the log:

Hosting environment: Production
Content root path: d:\apps\FreightRates
Now listening on: http://localhost:9657
Application started. Press Ctrl+C to shut down.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://auqldrv00dev1si:10000/  

So it is starting, but the environment is being reported as Production. Don't know where that is coming from because the ASPNETCORE_Environment=DEV. I don't use the term "Production" anywhere in my code, I have dbl-checked. We use the term "PRD". I even updated the default template code to use UAT,PRD instead of Staging, Production in all the places it appears (_Layout.cshtml, etc).

I have raised a stackoverflow question outlining in more detail the issue.

I appears that the system environment variables are being completely ignored. I thought initially that it may be a permission issue, but I changed the Application Pool in IIS to run as me, but still not.

Can anyone point me in a direction to look for the issue? Or that I can respond to to further clarify the setup.

Allann avatar Dec 07 '16 04:12 Allann

It might be useful to dump all environment variables in your app

davidfowl avatar Dec 07 '16 04:12 davidfowl

i did a SET command from a command prompt on that server to confirm that the variables existed and where set correctly. They were.

Allann avatar Dec 07 '16 04:12 Allann

@Allann I mean writing code in the application itself that dumps the environment variables.

davidfowl avatar Dec 07 '16 05:12 davidfowl

IIS doesn't pick up environment variable changes without restarting the service or machine. Note Production is the default value.

Tratcher avatar Dec 07 '16 05:12 Tratcher

You shouldn't have to restart the machine to see new env vars. Recycling the application pool should be enough.

davidfowl avatar Dec 07 '16 05:12 davidfowl

I just noticed that the ApplicationPool instance had LoadUserProfile =false, setting it to true made the site work as expected and the variables were loaded. Now to change the deployment script that creates the apppool to ensure that value is set to true.

I was a little surprised I had to load the user profile for machine variables.

Thanks for the help.

Allann avatar Dec 07 '16 05:12 Allann

Mine has LoadUserProfile = true. Still not working.

smiller781 avatar Jan 04 '18 20:01 smiller781

Setting LoadUserProfile = true worked for me also. Is there any documentation for this?

kakins avatar Feb 21 '18 01:02 kakins

Special Thanks to @Allann! your workaround still works in ASP.NET Core 2.2.4

mirsaeedi avatar Jul 05 '19 22:07 mirsaeedi