1

Have been looking for the right way to add configurations to my dot net core 2.0 web API.

Until now what I have done is:

  1. Added appsetteings.Development.json, appsetteings.Production.json

  2. In program.cs:

    public static IWebHost BuildWebHost(string[] args)
    {
        return WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .ConfigureAppConfiguration((hostContext, config) =>
            {
                var env = hostContext.HostingEnvironment;
                config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
            })
            .Build();
    }
    
  3. In startup.cs

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.Configure<dynamic>(Configuration);
    }
    

The issue is whenever I debug my code it always takes settings from appsettings.Development.json.

I also found that the hostContext.HostingEnvironment.EnvironmentName always comes as development regardless of which environment I pick to debug in.

1

1 Answer 1

2

There is a setting with the project properties that is causing the Development environment setting to be used during debug.

Open Project properties Navigate to the Debug page In the Environment Variables section you will see:

ASPNETCORE_ENVIRONMENT | Development

enter image description here

If you remove this flag and debug you app it should be running without the Development settings.

The official documentation can be found here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, that was very helpful.
What if it's a Console app?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.