ASP.NET Core 项目启动,默认执行顺序为:宿主 Host -> 读取配置 -> 日志设置 -> 注册服务(DI)-> 添加中间件 -> WebHost 监听 -> 后台 Work 启动。
配置的加载和读取是在启动流程的最前面。微软关于 ASP.NET Core 中的配置文档:https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/configuration/?view=aspnetcore-6.0
Host.CreateDefaultBuilder 方法中,按照以下顺序为应用提供默认配置:
- ChainedConfigurationProvider:添加现有的 作为源。 在默认配置示例中,添加主机配置,并将它设置为应用配置的第一个源。
- 使用 appsettings.json的 appsettings.json。
- 使用 JSON 配置提供程序通过 appsettings..json 提供。 例如,appsettings.Production.json 和 appsettings.Development.json。
- 应用在 环境中运行时的应用机密。
- 使用环境变量配置提供程序通过环境变量提供。
- 使用命令行配置提供程序通过命令行参数提供。
源码如下:
public static IHostBuilder CreateDefaultBuilder(string[] args)
{
var builder = new HostBuilder();
builder.UseContentRoot(Directory.GetCurrentDirectory());
builder.ConfigureHostConfiguration(config =>
{
config.AddEnvironmentVariables(prefix: "DOTNET_");
if (args != null)
{
config.AddCommandLine(args);
}
});
builder.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
if (env.IsDevelopment() && !string.IsNullOrEmpty(env.ApplicationName))
{
var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
if (appAssembly != null)
{
config.AddUserSecrets(appAssembly, optional: true);
}
}
config.AddEnvironmentVariables();
if (args != null)
{
config.AddCommandLine(args);
}
})
.ConfigureLogging((hostingContext, logging) =>
{
var isWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
if (isWindows)
{
logging.AddFilter<EventLogLoggerProvider>(level => level >= LogLevel.Warning);
}
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.AddDebug();
logging.AddEventSourceLogger();
if (isWindows)
{
logging.AddEventLog();
}
})
.UseDefaultServiceProvider((context, options) =>
{
var isDevelopment = context.HostingEnvironment.IsDevelopment();
options.ValidateScopes = isDevelopment;
options.ValidateOnBuild = isDevelopment;
});
return builder;
}
源码地址:https://github.com/dotnet/extensions/blob/release/3.1/src/Hosting/Hosting/src/Host.cs

通过代码可以看出,程序获取配置优先级依次为:appsettings.json -> appsettings.环境.json -> 环境变量 -> 命令行参数。我们根据优先级进行测试。
新建一个控制台方法返回所有配置信息,代码如下:
private readonly ILogger<HomeController> _logger;
public IConfiguration _configuration { get; }
public HomeController(ILogger<HomeController> logger, IConfiguration configuration)
{
_logger = logger;
_configuration = configuration;
}
public IActionResult Index()
{
return Json(_configuration.AsEnumerable());
}
首先,appsettings.json 配置文件,如下:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"WebConfig": {
"Name": "itsvse.com",
"Date": "2021"
}
}
新建 appsettings.Test.json 配置,如下:
{
"WebConfig": {
"Name": "itsvse.com test"
}
}
尝试启动项目,查看 WebConfig:Name 和 WebConfig:Date 的配置,如下图:

找到 Properties -> launchSettings.json 文件,修改 ASPNETCORE_ENVIRONMENT 环境配置为 Test,如下:
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Test"
}
}
这时程序会读取 appsettings.Test.json 的配置,尝试重新启动项目,发现 WebConfig:Name 已经覆盖了,如下图:

再次修改 launchSettings.json 文件,通过环境变量设置 WebConfig:Name 的值,代码如下:
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Test",
"WebConfig__Name": "itsvse env"
}
}
备注:环境变量修改 WebConfig:Name 的值,变量名称为: WebConfig__Name (中间用双下划线隔开)
尝试重启项目,发现 WebConfig:Name 的值,已经被环境变量设置的值所覆盖,如下图:

尝试通过命令行的形式修改默认配置的值,启动命令如下:
dotnet run --WebConfig:Name="itsvse command"
如下图:

用实践来测试配置键值的优先级,完。
|
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示