1

We have .NET Core 2.2 Web API project and we use the following code to load the appropriate appsettings.json file based on the DEBUG or RELEASE build flags.

public static IWebHost BuildWebHost(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseConfiguration(new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
#if DEBUG
        .AddJsonFile("appsettings.Development.json")
#endif
#if RELEASE
        .AddJsonFile("appsettings.Production.json")
#endif
        .AddJsonFile("appsettings.json")
        .Build()
    )
    .UseStartup<Startup>()
    .Build();

We created an external project, which calls the same method inside a Topshelf Windows Service project.

The strange thing is that the appsettings.Production.json file is always loaded, regardless of whether we are debugging or releasing the project.

1
  • what values does appsettings.json have? production or dev? Commented Mar 12, 2019 at 20:30

2 Answers 2

4

Take a look at the documentation for CreateDefaultBuilder()

Remarks

The following defaults are applied to the returned WebHostBuilder:

  • use Kestrel as the web server and configure it using the application's configuration providers,

  • set the ContentRootPath to the result of GetCurrentDirectory(),

  • load IConfiguration from appsettings.json and appsettings.[EnvironmentName].json,

  • load IConfiguration from User Secrets when EnvironmentName is 'Development' using the entry assembly,

  • load IConfiguration from environment variables,

  • load IConfiguration from supplied command line args,

  • configure the ILoggerFactory to log to the console and debug output,

  • and enable IIS integration.

Number 3 in that list is always going to look at the value of the ASPNETCORE_ENVIRONMENT environment variable (defaults to "Production" if not specified), and attempt to load an appsettings file with that name.

Rather than change your code, or using preprocessor directives, just change the value of that environment variable (to, for example, "Development").

This is how your launchSettings.json file works:

"profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
  ...

Don't fight with CreateDefaultBuilder() - your posted code does so many steps that the method already does for you (loading files, setting the base path, etc).

This is the default Program.cs given to you with ASP.Net Core projects, and it will work for you just fine:

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

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();
}

Also, just a note, you're loading environment specific files before the main appsettings.json file. Usually you'll want to do that in the other order.

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

Comments

1

Do something like the following and set the environment variable in the hosting system OS then:

var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

public static IWebHost BuildWebHost(string[] args) =>
  WebHost
    .UseConfiguration(new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())

    .AddJsonFile($"appsettings.json", true, true)
    .AddJsonFile($"appsettings.{environmentName}.json", true, true)

    .Build()
  )
  .UseStartup<Startup>()
  .Build();

Edit: Removed CreateDefaultBuilder()

Take a look at https://andrewlock.net/exploring-program-and-startup-in-asp-net-core-2-preview1-2/#setting-up-app-configuration-in-configureappconfiguration

1 Comment

That's essentially what CreateDefaultBuilder already does.

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.