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.
It might be useful to dump all environment variables in your app
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 I mean writing code in the application itself that dumps the environment variables.
IIS doesn't pick up environment variable changes without restarting the service or machine. Note Production is the default value.
You shouldn't have to restart the machine to see new env vars. Recycling the application pool should be enough.
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.
Mine has LoadUserProfile = true. Still not working.
Setting LoadUserProfile = true worked for me also. Is there any documentation for this?
Special Thanks to @Allann! your workaround still works in ASP.NET Core 2.2.4
Most helpful comment
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.