国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

asp.net core配置文件加載過程的深入了解

2024-07-10 12:48:36
字體:
供稿:網(wǎng)友

前言

配置文件中程序運行中,擔當著不可或缺的角色;通常情況下,使用 visual studio 進行創(chuàng)建項目過程中,項目配置文件會自動生成在項目根目錄下,如 appsettings.json,或者是被大家廣泛使用的 appsettings.{env.EnvironmentName}.json;配置文件

作為一個入口,可以讓我們在不更新代碼的情況,對程序進行干預和調(diào)整,那么對其加載過程的全面了解就顯得非常必要。

何時加載了默認的配置文件

在 Program.cs 文件中,查看以下代碼

 public class Program {  public static void Main(string[] args)  {   CreateWebHostBuilder(args).Build().Run();  }  public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>   WebHost.CreateDefaultBuilder(args)    .UseStartup<Startup>(); }

WebHost.CreateDefaultBuilder 位于程序集 Microsoft.AspNetCore.dll 內(nèi),當程序執(zhí)行 WebHost.CreateDefaultBuilder(args) 的時候,在 CreateDefaultBuilder 方法內(nèi)部加載了默認的配置文件

代碼如下

public static IWebHostBuilder CreateDefaultBuilder(string[] args)  {   var builder = new WebHostBuilder();   if (string.IsNullOrEmpty(builder.GetSetting(WebHostDefaults.ContentRootKey)))   {    builder.UseContentRoot(Directory.GetCurrentDirectory());   }   if (args != null)   {    builder.UseConfiguration(new ConfigurationBuilder().AddCommandLine(args).Build());   }   builder.UseKestrel((builderContext, options) =>    {     options.Configure(builderContext.Configuration.GetSection("Kestrel"));    })    .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())     {      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) =>    {     logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));     logging.AddConsole();     logging.AddDebug();     logging.AddEventSourceLogger();    })    .ConfigureServices((hostingContext, services) =>    {     // Fallback     services.PostConfigure<HostFilteringOptions>(options =>     {      if (options.AllowedHosts == null || options.AllowedHosts.Count == 0)      {       // "AllowedHosts": "localhost;127.0.0.1;[::1]"       var hosts = hostingContext.Configuration["AllowedHosts"]?.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);       // Fall back to "*" to disable.       options.AllowedHosts = (hosts?.Length > 0 ? hosts : new[] { "*" });      }     });     // Change notification     services.AddSingleton<IOptionsChangeTokenSource<HostFilteringOptions>>(      new ConfigurationChangeTokenSource<HostFilteringOptions>(hostingContext.Configuration));     services.AddTransient<IStartupFilter, HostFilteringStartupFilter>();    })    .UseIIS()    .UseIISIntegration()    .UseDefaultServiceProvider((context, options) =>    {     options.ValidateScopes = context.HostingEnvironment.IsDevelopment();    });   return builder;  }            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 大连市| 海丰县| 图木舒克市| 富锦市| 绥化市| 体育| 开平市| 建水县| 玉环县| 万源市| 库车县| 江华| 科尔| 申扎县| 河北省| 通州区| 囊谦县| 建瓯市| 兰西县| 铜川市| 天长市| 宜兰县| 建湖县| 游戏| 遂昌县| 咸宁市| 台中市| 北宁市| 柘荣县| 鄱阳县| 攀枝花市| 临泉县| 蛟河市| 乐陵市| 梧州市| 农安县| 西贡区| 祁东县| 淳安县| 灵川县| 清苑县|