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

Posted lvshunbin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.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, startupType, requiredService.EnvironmentName));
16                     );
17                 
18             );
19         
View Code

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

请看代码:

技术图片

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

以上是关于.net core mvc启动顺序以及主要部件2的主要内容,如果未能解决你的问题,请参考以下文章

ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 14. ASP.NET Core Identity 入门

ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 25. 过滤器

[Asp.Net Core]MVC_Razor布局

[Asp.Net Core]MVC_Razor布局

[MVC&Core]ASP.NET Core MVC 视图传值入门

asp.net core mvc在centOs8启动后丢失了样式