Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core ignoring ASPNETCORE_ENVIRONMENT variable

I know that others have posted this problem, but none of the solutions I've seen appear in my case. I have an ASP.NET WebAPI application. I've been able to publish it to two different development servers, via Visual Studio Publish. On both target servers, I set the ASPNETCORE_ENVIRONMENT environment variable to Development via the Windows GUI and the app picks this up, uses my appSettings.Development.json settings, and works as expected.

Now I'm deploying it to my Test server. On that server I set ASPNETCORE_ENVIRONMENT environment variable to Test via the Windows GUI, but when the application runs, it always says Hosting Environment: Production. I've checked my launchSettings.json file as one post suggested, but nothing in there sets the environment variable to Production. Since Production is the default, I don't have code that sets that value anywhere.

My successful deployment to my Dev server and my failed deployment to my Test server were both done via dotnet publish executed from Bamboo. I don't know why the application running on my Test server is not picking up the ASPNETCORE_ENVIRONMENT Test setting.

A few other details that I thought of over the weekend, which may or may not be relevant.

VS Publish to Windows Server 2008 R2, debug build, works as expected. Target server has ASPNETCORE_ENVIRONMENT set to Development, application log reports Hosting environment: Development, and appSettings.Development.json settings are in effect.

Both VS Publish and Bamboo deploy (via dotnet publish) to Windows Server 2016, debug build, works as expected. I have two instances of the site on this server. One relies on the ASPNETCORE_ENVIRONMENT variable, which is set to Development, and works as expected. The other overrides the environment variable for its app pool via a setting in applicationHost.config. In both cases, the applications correctly pick up the environment variable and reflect the correct hosting environment.

Bamboo deploy via dotnet publish to Windows Server 2012, release build, does not work as expected. Application reports the hosting environment as Production (the default), suggesting that it failed to pick up the environment variable I set. The application fails because it is missing the app settings from appSettings.Development.json.

So the failing scenario is a different server O/S, but since it works on both 2008 and 2016, I would think it would work on 2012. And the failing scenario is a release build vs. a debug build, but I wouldn't think that would impact the runtime behavior re configuration.

I don't think it's a launchSettings.json issue, because that would presumably affect all of my target deployments, not just my Test server. I have confirmed via command line that the environment variable is correctly set. I don't really want a web.config-based solution, which I don't think I should need and which will require environment-specific transforms.

Some more things I've tried that haven't helped: Changed the build type from Release to Debug (since it is Debug in the environments that are working). Still doesn't work. Tried logging other environment variables, but application dies before it gets to my logging code. Still working on this... Tried setting the environment variable in applicationHost.config as I'd done on my Development server, but Test server is Windows Server 2012 and IIS 8 and does not support the element in applicationHost.config. (Dev server is Windows Server 2016 and IIS 10, which does support the element in applicationHost.config.) Tried VS Publish vs. Bamboo deploy. Still doesn't work. I compared .NET Core installations on the Dev and Test servers. Both have host version 2.2.3, same commit. Dev server has SDKs installed; Test server does not. Dev server has multiple runtime versions installed, latest being 2.2.3. Test server has only 2.2.3 runtime installed. The main difference seems to be the SDKs and I assume I shouldn't need the SDK installed in order to read the environment variables.

like image 570
Zoe Avatar asked Mar 05 '23 07:03

Zoe


1 Answers

I think I've hit this and solved it before. I had to set LoadUserProfile to True in my IIS app pool advanced settings. I'm not sure why this is true because things I've read have said that you need this to read user environment variables and my app is failing to read system environment variables. But, whatever the case, when I set LoadUserProfile to true on my IIS app pool, the app correctly reads the ASPNETCORE_ENVIRONMENT system envrionment variable and the app works as expected.

like image 79
Zoe Avatar answered Mar 07 '23 01:03

Zoe