Thaoden
Thaoden

Reputation: 3580

ASP.NET Core hosting environment variable ignored

I have two web sites on my staging server, and both are ASP.NET Core sites that run in IIS. I have set the environment variable ASPNETCORE_ENVIRONMENT to Staging machine-wide. This works well for one of the sites, however the other ignores the variable and runs in production mode instead. I have to configure the hosting environment into the web.config file to run it in staging mode.

Why does one site not take the environment variable into account?

In both of my Startup(IHostingEnvironment env) constructors, I use the 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();
    }

Upvotes: 29

Views: 21005

Answers (5)

spottedmahn
spottedmahn

Reputation: 16031

From a cmd window, run net stop /y was && net start w3svc.

Source: ASP.NET Core Docs -> Use multiple environments in ASP.NET Core -> Windows - IIS deployments


Note: Restarting IIS via right click-> stop -> start in IIS Manager will NOT work.

Upvotes: 8

Sean
Sean

Reputation: 379

If you are debugging your code in Visual Studio, bear in mind that Visual Studio caches the environment variables that were present when Visual Studio was started - not when you hit "debug"!

So you may have to restart Visual Studio for any changes to the environment to be visible.

Upvotes: 16

Thaoden
Thaoden

Reputation: 3580

As said in this similar question, the trick was simply to set the app pool to load the user variables (IIS -> Server -> App Pools -> Right click on pool -> Set application pool defaults... -> Load User Profile = True).

I configured only one of my app pools accordingly, thus only one of the sites could access the environment variables.

Upvotes: 33

Chris Pirie
Chris Pirie

Reputation: 421

I just spent the last couple hours dealing with the same issue. I'm not sure if the result will be the same since you seem to have one of two applications working.

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

Using "set" from Command Prompt showed the expected results of "ASPNETCORE_ENVIRONMENT=Staging".

Calling Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") returned null. I created another variable called "Test" which also returned null. Calling any other existing variable returned expected results.

I tried recycling the application pool, changing the app pool's user, restarting IIS through management console, even restarting World Wide Web Publishing Service (probably same as reset in IIS mgmt console) all to no avail.

Rebooting the server was the only way I could get the application to return the expected result.

Upvotes: 23

Jagmeet Kumar
Jagmeet Kumar

Reputation: 9

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 might not have added variable builder.AddEnvironmentVariables();

Upvotes: -6

Related Questions