Asp.Net Core Host environment variable ignored

I have two websites on my staging server, both are Asp.net Core sites that run on IIS. I set the ASPNETCORE_ENVIRONMENT environment ASPNETCORE_ENVIRONMENT to Staging throughout the machine. This works well for one of the sites, however the other ignores the variable and works in production mode. I need to configure the hosting environment in the web.config in order to run it in intermediate mode.

Any hints as to why one site does not account for the environment variable?

EDIT: In both my Startup(IHostingEnvironment env) constructors Startup(IHostingEnvironment env) I use environment variables:

 public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) .AddJsonFile("logging.json") .AddEnvironmentVariables(); // <--- Configuration = builder.Build(); } 
+6
source share
3 answers

As said in this similar question , the trick was to simply configure the application pool to load user variables (IIS β†’ Server β†’ App Pools β†’ Right, click on the pool β†’ Set default application pool ... β†’ Download user profile = True).

I configured only one of my application pools, so only one of the sites could access the environment variables.

+9
source

I just spent the last couple of hours on the same issue. I'm not sure that the result will be the same, since you seem to be running one of two applications.

I set ASPNETCORE_ENVIRONMENT to "Staging" as a system variable through "Advanced System Settings" in Windows Server 2008 R2 and always ended up in the "Production" environment (which is the default environment if it cannot find the setting anywhere).

Using "set" from the command line showed the expected results "ASPNETCORE_ENVIRONMENT = Staging".

A call to Environment.GetEnvironmentVariable ("ASPNETCORE_ENVIRONMENT") returns null. I created another variable called "Test", which also returned zero. A call to any other existing variable returned the expected results.

I tried reusing the application pool by changing the application pool user, restarting IIS through the management console, even restarting the World Wide Web publishing service (possibly the same as reset in the IIS mgmt IIS console), all to no avail.

Rebooting the server was the only way to force the application to return the expected result.

+4
source
 public Startup(IHostingEnvironment env) { var builder = new ConfigurationBuilder() .SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true); if (env.IsDevelopment()) { // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709 builder.AddUserSecrets(); } builder.AddEnvironmentVariables(); Configuration = builder.Build(); } 

You may not have added the builder.AddEnvironmentVariables(); variable builder.AddEnvironmentVariables();

-3
source

All Articles