Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Startup.cs returns wrong environment

The Startup class contains

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);

    Console.WriteLine($"{env.EnvironmentName.ToString()}");

    if (env.IsDevelopment())
    {
        // For more details on using the user secret store see 
        // https://go.microsoft.com/fwlink/?LinkID=532709
        builder.AddUserSecrets();
    }

    builder.AddEnvironmentVariables();
    Configuration = builder.Build();
}

but env.EnvironmentName.ToString() returns "Production".

I already setup my ASPNETCORE_ENVIRONMENT to "Development" in launchSettings.json

like image 808
datkom Avatar asked Oct 09 '16 14:10

datkom


1 Answers

This usually happens when you have setup environment in web.config too.

For example, if you have environment setup as Production in launchSettings.json-

  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      }
    },

And in web.config, if you have other environment Staging-

<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false">
  <environmentVariables>
    <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Staging" />
  </environmentVariables>
</aspNetCore>

In this case, you will get Staging when you are trying to read env.EnvironmentName in startup.cs

See if this helps.

like image 184
Sanket Avatar answered Nov 09 '22 23:11

Sanket