ASP.NET Core hosting environment variable ignored
Asked Answered
A

5

29

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();
    }
Agony answered 17/10, 2016 at 11:21 Comment(5)
just to double check - you sure, that ASPNETCORE_ENVIRONMENT=Staging on second instance, but env.EnvironmentName returns "Production" for it and values from appsettings.Staging.json are not used? By the way don't you forget to publish appsettings.Staging.json file?Odaodab
I don't use appsettings.Staging.json on either site. ASPNETCORE_ENVIRONMENT is set machine-wide. Startup logging for second site logs: "Environment: Production".Agony
could you check what Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") returns?Odaodab
[09:20:33 INF] ASPNETCORE_ENVIRONMENT = '' Hosting environment: Production when starting via IIS, [08:56:07 INF] ASPNETCORE_ENVIRONMENT = 'Staging' Hosting environment: Staging when starting stand-alone directly on console.Agony
Possible duplicate of IIS doesn't use user environment variablesHurst
A
33

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.

Agony answered 18/10, 2016 at 7:47 Comment(4)
I've tried this for my similar problem and it does not fix for me.Herisau
Worked for me too. I'm wondering why setting the environment variable as a machine variable didn't work..Real
Worked for me too, very strange according to MS doco machine env var should work..Failsafe
don't use set, but setx when setting the env var from the command line. and add /M to set machine wide, not for local user, because the app pool could use another userEburnation
Z
23

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.

Zaria answered 18/10, 2016 at 6:2 Comment(2)
holy sxxx, wasted 30 minutes of my time for this issue, thank you for your postSmock
I can also confirm that only restarting the server worked. Since it's system variable only after restarting the server takes new variable.Dombrowski
R
16

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.

Roadability answered 9/8, 2018 at 13:25 Comment(0)
B
6

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

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


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

Bradleigh answered 20/9, 2018 at 17:17 Comment(1)
the article says restating is the second optionEburnation
S
-6
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();

Stripling answered 17/10, 2016 at 11:33 Comment(4)
I checked this, it is there in both projects.Agony
AddEnvironmentVariables() does not change the value of env.EnvironmentName. It allows ro read configuration values from environment variables.Odaodab
Does IsDevelopment() work without AddEnvironmentVariables()?Agony
@Agony IsDevelopment is just wrapper around env.EnvironmentName == "Development". env.EnvironmentName by default is set internally using ASPNETCORE_ENVIRONMENT. As IHostingEnvironment.EnvironmentName has a getter, the value can be changed directly in your code, but AddEnvironmentVariables() does not change that value, Instead it allows to use environment variables as additionla source for Configuration.Odaodab

© 2022 - 2024 — McMap. All rights reserved.