.net core mvc啟動順序以及主要部件2

来源:https://www.cnblogs.com/lvshunbin/archive/2019/06/23/11072531.html
-Advertisement-
Play Games

前一篇提到WebHost.CreateDefaultBuilder(args)方法創建了WebHostBuilder實例,WebHostBuilder實例有三個主要功能 1、構建了IConfiguration實例和基礎環境配置,2、構建了IServiceCollection服務,也就是依賴註入的容器 ...


前一篇提到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中的主要的構建方法和調用Startup中的ConfigureServices方法,而Run則是正式啟動應用程式

請看代碼:

分享Program類的過程大致就到這裡了


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 目錄 "引言" "控制反轉" "讀寫分離分庫分表" "理論基礎" "設計目標" "現狀調研" "設計思路" "實現之過五關斬六將" "動態對象" "動態模型緩存" "數據源移植" "查詢表達式樹深度移植" "數據合併演算法" "事務支持" "實際使用" "展望未來" 引言 前方硬核警告:全文乾貨110 ...
  • 問題 在一個 Timer 時鐘的周期方法內,有一個 迴圈,當執行到某些特定的語句時,會從頭開始執行,而不會執行後續的語句。 分析 該問題我在初步分析之後,發現可能是由於程式出現了異常,但是被吞噬掉造成的。在我某一個 item 元素當中,其值為 應該會拋出空異常。但是沒有異常拋出,後續我不知道哪個地方 ...
  • 公司的產品一直緊跟 .net core 3.0 preview 不斷升級, 部署到 Linux 伺服器後, 偶爾會出現某個進程CPU占用100%. 由於服務部署在雲上, 不能使用遠程調試; 在區域網內的Linux 伺服器 或 Windows開發機上又不能重現這個問題, 聯想到Java的jstack, ...
  • 當代碼提交到GitHub後,自動生成構建項目並部署到伺服器。接下來介紹一下如何在容器中運行Jenkins,並自動化構建GitHub上的項目,使用自動化構建來解放你的雙手。一臺已經安裝了Docker的Ubuntu伺服器,有公網IP並可通過IP或者功能變數名稱訪問。因為GitHub無法訪問本地伺服器,所以不能觸... ...
  • 在Asp.Net網站開發的過程中,很多時候會遇到對象的序列化和反序列化操作,Newtonsoft.Json組件是專門用來序列化和反序列化操作的一個功能組件,引入這個DLL組件後,就可使用JsonConvert.DeserializeObject方法來反序列化字元串為對象,JsonConvert.De ...
  • 在Asp.Net網站開發的過程中,很多時候會遇到對象的序列化和反序列化操作,Newtonsoft.Json組件是專門用來序列化和反序列化操作的一個功能組件,引入這個DLL組件後,就可使用JsonConvert.SerializeObject方法來序列化C#的對象。JsonConvert.Serial ...
  • 在C#的List集合操作中,Last方法和LastOrDefault方法都會用來查找集合中最後一個符合條件的元素對象,但Last和LastOrDefault方法還是有差別的,建議使用LastOrDefault方法來操作,因為Last方法在未查到一條符合條件的對象的時候,將會直接拋出程式運行時異常。 ...
  • 一直想看看這部分內容,今天終於看到了。 vs2017調試通過。 記錄如下: 1、添加需要的provider,有點添加驅動的意思。右擊項目,NUGET “瀏覽”,“JetEntityFrameworkProvider”,安裝,如圖 完成後配置文件(控制台為app.config)中就會有這個驅動: 最好 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...