网站行程表怎么做,wordpress多域名插件,哪些网站做的美剧,个人网站素材图片ASP.NET Core 项目启动#xff0c;默认执行顺序为#xff1a;宿主 Host - 读取配置 - 日志设置 - 注册服务#xff08;DI#xff09;- 添加中间件 - WebHost 监听 - 后台 Work 启动。配置的加载和读取是在启动流程的最前面。微软关于 ASP.NET Core… ASP.NET Core 项目启动默认执行顺序为宿主 Host - 读取配置 - 日志设置 - 注册服务DI- 添加中间件 - WebHost 监听 - 后台 Work 启动。配置的加载和读取是在启动流程的最前面。微软关于 ASP.NET Core 中的配置文档https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/configuration/?viewaspnetcore-6.0Host.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);// IMPORTANT: This needs to be added *before* configuration is loaded, this lets// the defaults be overridden by the configuration.if (isWindows){// Default the EventLogLoggerProvider to warning or abovelogging.AddFilterEventLogLoggerProvider(level level LogLevel.Warning);}logging.AddConfiguration(hostingContext.Configuration.GetSection(Logging));logging.AddConsole();logging.AddDebug();logging.AddEventSourceLogger();if (isWindows){// Add the EventLogLoggerProvider on windows machineslogging.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 ILoggerHomeController _logger;public IConfiguration _configuration { get; }public HomeController(ILoggerHomeController 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:Nameitsvse command如下图用实践来测试配置键值的优先级完。