新建项目
新建ASP.NET Core Web 应用程序
Configuration加载顺序
namespace Microsoft.Extensions.Hosting
{
//
// 摘要:
// Provides convenience methods for creating instances of Microsoft.Extensions.Hosting.IHostBuilder
// with pre-configured defaults.
public static class Host
{
//
// 摘要:
// Initializes a new instance of the Microsoft.Extensions.Hosting.HostBuilder class
// with pre-configured defaults.
//
// 返回结果:
// The initialized Microsoft.Extensions.Hosting.IHostBuilder.
//
// 言论:
// The following defaults are applied to the returned Microsoft.Extensions.Hosting.HostBuilder:
// • set the Microsoft.Extensions.Hosting.IHostEnvironment.ContentRootPath to the
// result of System.IO.Directory.GetCurrentDirectory
// • load host Microsoft.Extensions.Configuration.IConfiguration from "DOTNET_"
// prefixed environment variables
// • load app Microsoft.Extensions.Configuration.IConfiguration from 'appsettings.json'
// and 'appsettings.[Microsoft.Extensions.Hosting.IHostEnvironment.EnvironmentName].json'
// • load app Microsoft.Extensions.Configuration.IConfiguration from User Secrets
// when Microsoft.Extensions.Hosting.IHostEnvironment.EnvironmentName is 'Development'
// using the entry assembly
// • load app Microsoft.Extensions.Configuration.IConfiguration from environment
// variables
// • configure the Microsoft.Extensions.Logging.ILoggerFactory to log to the console,
// debug, and event source output
// • enables scope validation on the dependency injection container when Microsoft.Extensions.Hosting.IHostEnvironment.EnvironmentName
// is 'Development'
public static IHostBuilder CreateDefaultBuilder();
//
// 摘要:
// Initializes a new instance of the Microsoft.Extensions.Hosting.HostBuilder class
// with pre-configured defaults.
//
// 参数:
// args:
// The command line args.
//
// 返回结果:
// The initialized Microsoft.Extensions.Hosting.IHostBuilder.
//
// 言论:
// The following defaults are applied to the returned Microsoft.Extensions.Hosting.HostBuilder:
// • set the Microsoft.Extensions.Hosting.IHostEnvironment.ContentRootPath to the
// result of System.IO.Directory.GetCurrentDirectory
// • load host Microsoft.Extensions.Configuration.IConfiguration from "DOTNET_"
// prefixed environment variables
// • load host Microsoft.Extensions.Configuration.IConfiguration from supplied command
// line args
// • load app Microsoft.Extensions.Configuration.IConfiguration from 'appsettings.json'
// and 'appsettings.[Microsoft.Extensions.Hosting.IHostEnvironment.EnvironmentName].json'
// • load app Microsoft.Extensions.Configuration.IConfiguration from User Secrets
// when Microsoft.Extensions.Hosting.IHostEnvironment.EnvironmentName is 'Development'
// using the entry assembly
// • load app Microsoft.Extensions.Configuration.IConfiguration from environment
// variables
// • load app Microsoft.Extensions.Configuration.IConfiguration from supplied command
// line args
// • configure the Microsoft.Extensions.Logging.ILoggerFactory to log to the console,
// debug, and event source output
// • enables scope validation on the dependency injection container when Microsoft.Extensions.Hosting.IHostEnvironment.EnvironmentName
// is 'Development'
public static IHostBuilder CreateDefaultBuilder(string[] args);
}
}
注意这段注释
// • load app Microsoft.Extensions.Configuration.IConfiguration from 'appsettings.json'
// and 'appsettings.[Microsoft.Extensions.Hosting.IHostEnvironment.EnvironmentName].json'
// • load app Microsoft.Extensions.Configuration.IConfiguration from User Secrets
```csharp
public static 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>();
});
}
就是说Host.CreateDefaultBuilder()
加载Configuration
的顺序是
appsettings.json
appsettings.{envName}.json
usesecrets(secrets.json)
同一个节点信息如果在三个
.json
文件中都有定义,会依赖加载顺序,最后的会覆盖之前的
多环境配置
这里的设计思路是将全局通用配置放在`appsettings.json
中,不同环境根据需求配置自己的节点,也可以使用相同名称来覆盖appsettings.json
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information"
}
},
"AllowedHosts": "*"
}
appsettings.Development.json
{
"ConnectString": "Server=.;Database=WebApp01.Db.Dev;uid=sa;pwd=wpl19950815;"
}
appsettings.Sit.json
{
"ConnectString": "Server=.;Database=WebApp01.Db.Sit;uid=sa;pwd=wpl19950815;"
}
调试不同环境
通过设置 environmentVariables:ASPNETCORE_ENVIRONMENT
环境名称来切换使用不同环境的配置文件,这里环境变量名称需要和appsetting.{envName}.json
中{envName}
保持一致