微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

.net core mvc启动顺序以及主要部件2

原文: .net core mvc启动顺序以及主要部件2

前一篇提到WebHost.CreateDefaultBuilder(args)方法创建了WebHostBuilder实例,WebHostBuilder实例有三个主要功能 1、构建了IConfiguration实例和基础环境配置,2、构建了IServiceCollection服务,也就是依赖注入的容器,3、创建了webhost实例,这个webhost就是我们的接收请求的第一个管道,其中暴露出来的主要方法Build,请看源代码

分享图片

 1 public IWebHost Build()
 2         {
 3             if (!_webHostBuilt)
 4             {
 5                 _webHostBuilt = true;
 6                 AggregateException hostingStartupErrors;
 7                 IServiceCollection serviceCollection = BuildCommonServices(out hostingStartupErrors);
 8                 IServiceCollection serviceCollection2 = serviceCollection.Clone();
 9                 IServiceProvider hostingServiceProvider = _003CBuild_003Eg__GetProviderFromFactory_007C13_0(serviceCollection);
10                 if (!_options.SuppressstatusMessages)
11                 {
12                     if (Environment.GetEnvironmentvariable("Hosting:Environment") != null)
13                     {
14                         Console.WriteLine("The environment variable ‘Hosting:Environment‘ is obsolete and has been replaced with ‘ASPNETCORE_ENVIRONMENT‘");
15                     }
16                     if (Environment.GetEnvironmentvariable("ASPNET_ENV") != null)
17                     {
18                         Console.WriteLine("The environment variable ‘ASPNET_ENV‘ is obsolete and has been replaced with ‘ASPNETCORE_ENVIRONMENT‘");
19                     }
20                     if (Environment.GetEnvironmentvariable("ASPNETCORE_SERVER.URLS") != null)
21                     {
22                         Console.WriteLine("The environment variable ‘ASPNETCORE_SERVER.URLS‘ is obsolete and has been replaced with ‘ASPNETCORE_URLS‘");
23                     }
24                 }
25                 Addapplicationservices(serviceCollection2,hostingServiceProvider);
26                 WebHost webHost = new WebHost(serviceCollection2,hostingServiceProvider,_options,_config,hostingStartupErrors);
27                 try
28                 {
29                     webHost.Initialize();
30                     ILogger<WebHost> requiredService = webHost.Services.GetrequiredService<ILogger<WebHost>>();
31                     foreach (IGrouping<string,string> item in from g in _options.GetFinalHostingStartupAssemblies().GroupBy((string a) => a,StringComparer.OrdinalIgnoreCase)
32                                                                where g.Count() > 1
33                                                                select g)
34                     {
35                         requiredService.LogWarning($"The assembly {item} was specified multiple times. Hosting startup assemblies should only be specified once.");
36                     }
37                     return webHost;
38                 }
39                 catch
40                 {
41                     webHost.dispose();
42                     throw;
43                 }
44             }
45             throw new InvalidOperationException(Resources.WebHostBuilder_SingleInstance);
46         }
View Code

这个方法中有几个笔记重要的部分,下面我给标记一下

分享图片

调用BuildCommonServices私有方法主要是注入一些程序所需的基础组件,请看源代码

分享图片

 1  private IServiceCollection BuildCommonServices(out AggregateException hostingStartupErrors)
 2         {
 3             hostingStartupErrors = null;
 4             _options = new WebHostOptions(_config,Assembly.GetEntryAssembly()?.GetName().Name);
 5             if (!_options.PreventHostingStartup)
 6             {
 7                 List<Exception> list = new List<Exception>();
 8                 foreach (string item in _options.GetFinalHostingStartupAssemblies().distinct(StringComparer.OrdinalIgnoreCase))
 9                 {
10                     try
11                     {
12                         foreach (HostingStartupAttribute customAttribute in Assembly.Load(new AssemblyName(item)).GetCustomAttributes<HostingStartupAttribute>())
13                         {
14                             ((IHostingStartup)Activator.CreateInstance(customAttribute.HostingStartupType)).Configure(this);
15                         }
16                     }
17                     catch (Exception innerException)
18                     {
19                         list.Add(new InvalidOperationException("Startup assembly " + item + " Failed to execute. See the inner exception for more details.",innerException));
20                     }
21                 }
22                 if (list.Count > 0)
23                 {
24                     hostingStartupErrors = new AggregateException(list);
25                 }
26             }
27             string contentRootPath = ResolveContentRootPath(_options.ContentRootPath,AppContext.BaseDirectory);
28             _hostingEnvironment.Initialize(contentRootPath,_options);
29             _context.HostingEnvironment = _hostingEnvironment;
30             ServiceCollection serviceCollection = new ServiceCollection();
31             serviceCollection.AddSingleton(_options);
32             ((IServiceCollection)serviceCollection).AddSingleton((IHostingEnvironment)_hostingEnvironment);
33             ((IServiceCollection)serviceCollection).AddSingleton((Microsoft.Extensions.Hosting.IHostingEnvironment)_hostingEnvironment);
34             serviceCollection.AddSingleton(_context);
35             IConfigurationBuilder configurationBuilder = new ConfigurationBuilder().SetBasePath(_hostingEnvironment.ContentRootPath).AddConfiguration(_config);
36             foreach (Action<WebHostBuilderContext,IConfigurationBuilder> configureAppConfigurationBuilderDelegate in _configureAppConfigurationBuilderDelegates)
37             {
38                 configureAppConfigurationBuilderDelegate(_context,configurationBuilder);
39             }
40             IConfigurationRoot configurationRoot = configurationBuilder.Build();
41             ((IServiceCollection)serviceCollection).AddSingleton((IConfiguration)configurationRoot);
42             _context.Configuration = configurationRoot;
43             DiagnosticListener implementationInstance = new DiagnosticListener("Microsoft.AspNetCore");
44             serviceCollection.AddSingleton(implementationInstance);
45             ((IServiceCollection)serviceCollection).AddSingleton((DiagnosticSource)implementationInstance);
46             serviceCollection.AddTransient<IApplicationBuilderFactory,ApplicationBuilderFactory>();
47             serviceCollection.AddTransient<IHttpContextFactory,HttpContextFactory>();
48             serviceCollection.AddScoped<IMiddlewareFactory,MiddlewareFactory>();
49             serviceCollection.AddOptions();
50             serviceCollection.AddLogging();
51             serviceCollection.AddTransient<IStartupFilter,AutoRequestServicesstartupFilter>();
52             serviceCollection.AddTransient<IServiceProviderFactory<IServiceCollection>,DefaultServiceProviderFactory>();
53             serviceCollection.AddSingleton<ObjectPoolProvider,DefaultObjectPoolProvider>();
54             if (!string.IsNullOrEmpty(_options.StartupAssembly))
55             {
56                 try
57                 {
58                     Type startupType = StartupLoader.FindStartupType(_options.StartupAssembly,_hostingEnvironment.EnvironmentName);
59                     if (IntrospectionExtensions.GetTypeInfo(typeof(IStartup)).IsAssignableFrom(startupType.GetTypeInfo()))
60                     {
61                         serviceCollection.AddSingleton(typeof(IStartup),startupType);
62                     }
63                     else
64                     {
65                         serviceCollection.AddSingleton(typeof(IStartup),delegate (IServiceProvider sp)
66                         {
67                             IHostingEnvironment requiredService = sp.GetrequiredService<IHostingEnvironment>();
68                             return new ConventionBasedStartup(StartupLoader.LoadMethods(sp,startupType,requiredService.EnvironmentName));
69                         });
70                     }
71                 }
72                 catch (Exception source)
73                 {
74                     ExceptiondispatchInfo capture = ExceptiondispatchInfo.Capture(source);
75                     ((IServiceCollection)serviceCollection).AddSingleton((Func<IServiceProvider,IStartup>)delegate
76                     {
77                         capture.Throw();
78                         return null;
79                     });
80                 }
81             }
82             foreach (Action<WebHostBuilderContext,IServiceCollection> configureServicesDelegate in _configureServicesDelegates)
83             {
84                 configureServicesDelegate(_context,serviceCollection);
85             }
86             return serviceCollection;
87         }
View Code

这个方法中其中就包含了注入IConfiguration的代码和构建IServiceCollection容器的代码,当然还有一些其他的主要中间件注入啊IApplicationBuilderFactory,包括HttpContextFactory等等都在这方法里面被IServiceCollection容器管理,具体请看下图

分享图片

这里讲清楚之后,我们直接进入UseStartup方法中,这个方法是注入了Startup的实例,请看主要源代码

分享图片

 1 public static IWebHostBuilder UseStartup(this IWebHostBuilder hostBuilder,Type startupType)
 2         {
 3             string name = startupType.GetTypeInfo().Assembly.GetName().Name;
 4             return hostBuilder.UseSetting(WebHostDefaults.ApplicationKey,name).ConfigureServices(delegate (IServiceCollection services)
 5             {
 6                 if (IntrospectionExtensions.GetTypeInfo(typeof(IStartup)).IsAssignableFrom(startupType.GetTypeInfo()))
 7                 {
 8                     services.AddSingleton(typeof(IStartup),startupType);
 9                 }
10                 else
11                 {
12                     services.AddSingleton(typeof(IStartup),delegate (IServiceProvider sp)
13                     {
14                         IHostingEnvironment requiredService = sp.GetrequiredService<IHostingEnvironment>();
15                         return new ConventionBasedStartup(StartupLoader.LoadMethods(sp,requiredService.EnvironmentName));
16                     });
17                 }
18             });
19         }
View Code

上面只是被IServiceCollection所管理  并没有真正起作用,真正调用的是Main方法中的Build().Run(),这里的Build就是上面那个IWebHostBuilder中的主要的构建方法,而Run则是正式启动应用程序和调用Startup中的ConfigureServices方法

请看代码

分享图片

分享Program类的过程大致就到这里了

原文地址:https://www.jb51.cc/netcore/670851.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐