NET Core 3.1 console app is always in "Production" mode

Viewed 170

Here's my code

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;

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

    public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args).ConfigureAppConfiguration((hostingContext, config) =>
    {
        config.Sources.Clear();

        var env = hostingContext.HostingEnvironment;

        config.AddJsonFile("appsettings.json")
              .AddJsonFile($"appsettings.{env.EnvironmentName}.json");

        config.AddEnvironmentVariables();

        if (args != null)
        {
            config.AddCommandLine(args);
        }
    });
}
}

Also the project has appsettings.Development.json and appsettings.Production.json. In VS I set the env variable ASPNETCORE_ENVIRONMENT explicitly as Development, and as a result launchSettings.json looks like this

{
  "profiles": {
  "ConsoleApp1": {
  "commandName": "Project",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
}
  }
}

Nevertheless, when I runt the app in VS I always have env.EnvironmentName set as Production. I wonder why and how I should set environment in this case as Development.

0 Answers
Related