question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ASPNETCORE_Environment overrides cmd parameter

See original GitHub issue

Describe the bug

We’ve noticed a very strange behavior of hosting environment setting. When using ASPNETCORE_Environment="fromEnv" environment variable and --Environment=fromCmd command-line argument then for some reason the environment variable wins.

This is very inconsistent given that:

  1. ASPNETCORE_urls vs --urls - command-line argument wins 📗
  2. DOTNET_ENVIRONMENT vs --Environment - command-line argument wins 📗
  3. ASPNETCORE_ENVIRONMENT vs --Environment - environment variable wins 📙

To Reproduce

  1. (This one works well) When I don’t set any environment variables, the command-line argument is applied:
dotnet .\WebApplication_TestEnvironment.dll --Environment=fromCmd
Current environment: fromCmd
  1. (This one works well too) When I set DOTNET_ENVIRONMENT environment variable, the command-line argument wins:
$ENV:DOTNET_ENVIRONMENT="fromDotnetEnv"
dotnet .\WebApplication_TestEnvironment.dll --Environment=fromCmd
Current environment: fromCmd
  1. (This one doesn’t work) When I set ASPNETCORE_ENVIRONMENT environment variable, then hosting environment is defined by the environment variable and not by cmd:
$ENV:ASPNETCORE_ENVIRONMENT="fromAspEnv"
dotnet .\WebApplication_TestEnvironment.dll --Environment=fromCmd
Current environment: fromAspEnv

I am able to reproduce it on a newly created ASP.NET Core 3.1 API project. The only small change I did is writing the current environment to the Console .

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }

   ....................................................................

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            Console.WriteLine($"Current environment: {env.EnvironmentName}");

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }

Further technical details

dotnet --info
.NET Core SDK (reflecting any global.json):
 Version:   3.1.100
 Commit:    cd82f021f4

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.18362
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\3.1.100\

Host (useful for support):
  Version: 3.1.0
  Commit:  65f04fb6db

.NET Core SDKs installed:
  1.1.9 [C:\Program Files\dotnet\sdk]
  2.1.201 [C:\Program Files\dotnet\sdk]
  2.1.508 [C:\Program Files\dotnet\sdk]
  2.1.509 [C:\Program Files\dotnet\sdk]
  2.1.701 [C:\Program Files\dotnet\sdk]
  2.1.801 [C:\Program Files\dotnet\sdk]
  2.2.109 [C:\Program Files\dotnet\sdk]
  2.2.401 [C:\Program Files\dotnet\sdk]
  3.1.100 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
  Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.1.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.1.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 1.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 1.1.8 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.1.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:14 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
KoshelevScommented, Dec 3, 2020

Any news on this?

I’ve created the following extension method as a workaround, but it has not been battle-tested yet

        public static IWebHostBuilder FixEnvironmentParam(this IWebHostBuilder webBuilder, string[] args)
        {
            var configBuilder = new ConfigurationBuilder();
            configBuilder.AddCommandLine(args);
            var config = configBuilder.Build();

            var env = config[HostDefaults.EnvironmentKey];
            if (string.IsNullOrWhiteSpace(env))
            {
                return webBuilder;
            }

            return webBuilder.ConfigureAppConfiguration((host, _) => host.HostingEnvironment.EnvironmentName = env)
                             .UseEnvironment(env);
        }
0reactions
adityamandaleekacommented, Feb 1, 2023

Triage: this seems low priority and the current behavior is now documented better. Closing this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use multiple environments in ASP.NET Core
Environment values in launchSettings.json override values set in the system environment. To set the ASPNETCORE_ENVIRONMENT for the current ...
Read more >
c# - ASPNETCORE_ENVIRONMENT no longer overrides ...
According to the docs, ASPNETCORE_ENVIRONMENT is suppose to override the DOTNET_ENVIRONMENT environment variable.
Read more >
ASPNETCORE_ENVIRONMENT Variable in ASP.NET Core
ASPNETCORE_ENVIRONMENT is an environment variable identifies the runtime environment uses it to load different configurations.
Read more >
Multiple Ways To Set Hosting Environment In .NET ...
If both environment variables are defined, then ASPNETCORE_ENVIRONMENT takes priority and it overrides the DOTNET_ENVIRONMENT value.
Read more >
5 ways to set the URLs for an ASP.NET Core app
Command line arguments - Set the URLs with the --urls parameter when ... If you don't override them manually with UseUrls() , then...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found