简体   繁体   中英

.NET Core Web API: Automatically Loading Incorrect appsettings.json File

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM