.NET Core 3.1.1--CreateDefaultBuilder(args) 方法源码

 1 // Assembly location: C:Program FilesdotnetsharedMicrosoft.AspNetCore.App3.1.1Microsoft.Extensions.Hosting.dll
 2 
 3 using Microsoft.Extensions.Configuration;
 4 using Microsoft.Extensions.DependencyInjection;
 5 using Microsoft.Extensions.Logging;
 6 using Microsoft.Extensions.Logging.EventLog;
 7 using System;
 8 using System.IO;
 9 using System.Reflection;
10 using System.Runtime.InteropServices;
11 
12 namespace Microsoft.Extensions.Hosting
13 {
14   public static class Host
15   {
16     public static IHostBuilder CreateDefaultBuilder()
17     {
18       return Host.CreateDefaultBuilder((string[]) null);
19     }
20 
21     public static IHostBuilder CreateDefaultBuilder(string[] args)
22     {
23       HostBuilder hostBuilder = new HostBuilder();
24       hostBuilder.UseContentRoot(Directory.GetCurrentDirectory());
25       hostBuilder.ConfigureHostConfiguration((Action<IConfigurationBuilder>) (config =>
26       {
27         config.AddEnvironmentVariables("DOTNET_");
28         if (args == null)
29           return;
30         config.AddCommandLine(args);
31       }));
32       hostBuilder.ConfigureAppConfiguration((Action<HostBuilderContext, IConfigurationBuilder>) ((hostingContext, config) =>
33       {
34         IHostEnvironment hostingEnvironment = hostingContext.HostingEnvironment;
35         config.AddJsonFile("appsettings.json", true, true).AddJsonFile("appsettings." + hostingEnvironment.EnvironmentName + ".json", true, true);
36         if (hostingEnvironment.IsDevelopment() && !string.IsNullOrEmpty(hostingEnvironment.ApplicationName))
37         {
38           Assembly assembly = Assembly.Load(new AssemblyName(hostingEnvironment.ApplicationName));
39           if (assembly != (Assembly) null)
40             config.AddUserSecrets(assembly, true);
41         }
42         config.AddEnvironmentVariables();
43         if (args == null)
44           return;
45         config.AddCommandLine(args);
46       })).ConfigureLogging((Action<HostBuilderContext, ILoggingBuilder>) ((hostingContext, logging) =>
47       {
48         int num = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 1 : 0;
49         if (num != 0)
50           logging.AddFilter<EventLogLoggerProvider>((Func<LogLevel, bool>) (level => level >= LogLevel.Warning));
51         logging.AddConfiguration((IConfiguration) hostingContext.Configuration.GetSection("Logging"));
52         logging.AddConsole();
53         logging.AddDebug();
54         logging.AddEventSourceLogger();
55         if (num == 0)
56           return;
57         logging.AddEventLog();
58       })).UseDefaultServiceProvider((Action<HostBuilderContext, ServiceProviderOptions>) ((context, options) =>
59       {
60         bool flag = context.HostingEnvironment.IsDevelopment();
61         options.ValidateScopes = flag;
62         options.ValidateOnBuild = flag;
63       }));
64       return (IHostBuilder) hostBuilder;
65     }
66   }
67 }

  

原文地址:https://www.cnblogs.com/YourDirection/p/12446821.html