網站首頁 編程語言 正文
1.前言
ASP.NET Core應用程序可以配置和啟動主機(Host)。主機負責應用程序啟動和生命周期管理。通用主機用于無法處理HTTP請求的應用程序。通用主機的用途是將HTTP管道從Web主機API中分離出來,從而啟用更多的主機方案。 基于通用主機的消息、后臺任務和其他非HTTP工作負載可從橫切功能(如配置、依賴關系注入[DI]和日志記錄)中受益。通用主機是ASP.NET Core 2.1中的新增功能,不適用于Web承載方案。對于Web承載方案,請使用Web主機。通用主機將在未來版本中替換Web主機,并在HTTP和非HTTP方案中充當主要的主機API。
2.介紹
IHostedService是執行代碼的入口點。每個IHostedService實現都按照ConfigureServices中服務注冊的順序執行。主機啟動時,每個IHostedService上都會調用StartAsync,主機正常關閉時,以反向注冊順序調用StopAsync。
3.設置主機
IHostBuilder是供庫和應用程序初始化、生成和運行主機的主要組件:
public static async Task Main(string[] args) { var host = new HostBuilder().Build(); await host.RunAsync(); }
4.選項
HostOptions配置IHost的選項。
4.1關閉超時值
ShutdownTimeout設置StopAsync的超時值。默認值為5秒。Program.Main中的以下選項配置將默認值為5秒的關閉超時值增加至20秒:
var host = new HostBuilder().ConfigureServices((hostContext, services) => { services.Configure(option => { option.ShutdownTimeout = System.TimeSpan.FromSeconds(20); }); }) .Build();
5.默認服務
在主機初始化期間注冊以下服務:
- 環境 (IHostingEnvironment)
- HostBuilderContext
- 配置 (IConfiguration)
- IApplicationLifetime (ApplicationLifetime)
- IHostLifetime (ConsoleLifetime)
- IHost
- 選項 (AddOptions)
- 日志記錄 (AddLogging)
6.主機配置
主機配置的創建方式如下:
- 調用IHostBuilder上的擴展方法以設置“內容根”和“環境”。
- 從ConfigureHostConfiguration中的配置提供應用程序讀取配置。
- 應用程序鍵(名稱)、內容根、環境配置方式我就不多說了,跟上一篇Web主機配置是一樣的。
6.1ConfigureHostConfiguration
ConfigureHostConfiguration使用IConfigurationBuilder來為主機創建IConfiguration。主機配置用于初始化IHostingEnvironment,以供在應用程序的構建過程中使用。可多次調用ConfigureHostConfiguration,并得到累計結果。必須在ConfigureHostConfiguration中顯式指定應用程序所需的任何配置提供自身,包括:
- 文件配置(例如,來自hostsettings.json文件)。
- 環境變量配置。
- 命令行參數配置。
- 任何其他所需的配置提供程序。
通過使用SetBasePath指定應用程序的基本路徑,然后調用其中一個文件配置提供應用程序,可以啟用主機的文件配置。示例應用使用JSON文件hostsettings.json,并調用AddJsonFile來使用文件的主機配置設置。要添加主機的環境變量配置,請在主機生成器上調用 AddEnvironmentVariables。AddEnvironmentVariables接受用戶定義的前綴(可選)。示例應用程序使用前綴PREFIX_。當系統讀取環境變量時,便會刪除前綴。配置示例應用程序的主機后,PREFIX_ENVIRONMENT的環境變量值就變成environment密鑰的主機配置值。示例HostBuilder配置使用ConfigureHostConfiguration:
var host = new HostBuilder().ConfigureHostConfiguration(configHost => { configHost.SetBasePath(Directory.GetCurrentDirectory()); configHost.AddJsonFile("hostsettings.json", optional: true); configHost.AddEnvironmentVariables(prefix: "PREFIX_"); configHost.AddCommandLine(args); })
6.2ConfigureAppConfiguration
通過在IHostBuilder實現上調用ConfigureAppConfiguration創建應用程序配置。ConfigureAppConfiguration使用IConfigurationBuilder來為應用程序創建IConfiguration。可多次調用ConfigureAppConfiguration,并得到累計結果。應用程序使用上一次在一個給定鍵上設置值的選項。HostBuilderContext.Configuration中提供ConfigureAppConfiguration創建的配置,以供進行后續操作和在Services中使用。應用程序配置會自動接收ConfigureHostConfiguration提供的主機配置。示例應用配置使用ConfigureAppConfiguration:
var host = new HostBuilder().ConfigureAppConfiguration((hostContext, configApp) => { configApp.SetBasePath(Directory.GetCurrentDirectory()); configApp.AddJsonFile("appsettings.json", optional: true); configApp.AddJsonFile( $"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true); configApp.AddEnvironmentVariables(prefix: "PREFIX_"); configApp.AddCommandLine(args); })
6.3ConfigureServices
ConfigureServices將服務添加到應用程序的依賴關系注入容器。可多次調用ConfigureServices,并得到累計結果。托管服務是一個類,具有實現IHostedService接口的后臺任務邏輯。示例應用程序使用AddHostedService擴展方法向自身添加生命周期事件 LifetimeEventsHostedService和定時后臺任務TimedHostedService服務:
var host = new HostBuilder() .ConfigureServices((hostContext, services) => { if (hostContext.HostingEnvironment.IsDevelopment()) { // Development service configuration } else { // Non-development service configuration } services.AddHostedService(); services.AddHostedService (); })
6.4ConfigureLogging
ConfigureLogging添加了一個委托來配置提供的ILoggingBuilder。可以利用相加結果多次調用 ConfigureLogging。
var host = new HostBuilder().ConfigureLogging((hostContext, configLogging) => { configLogging.AddConsole(); configLogging.AddDebug(); })
6.4.1UseConsoleLifetime
UseConsoleLifetime偵聽Ctrl+C/SIGINT或SIGTERM并調用StopApplication來啟動關閉進程。UseConsoleLifetime解除阻止RunAsync和WaitForShutdownAsync等擴展。ConsoleLifetime預注冊為默認生命周期實現,使用注冊的最后一個生命周期。
var host = new HostBuilder().UseConsoleLifetime()
7.容器配置
主機可以接受IServiceProviderFactory
為應用程序創建服務容器并提供服務容器工廠:
public class GenericHostSample { internal class ServiceContainerFactory : IServiceProviderFactory{ public ServiceContainer CreateBuilder(IServiceCollection services) { return new ServiceContainer(); } public IServiceProvider CreateServiceProvider(ServiceContainer containerBuilder) { throw new NotImplementedException(); } } }
使用該工廠并為應用程序配置自定義服務容器:
var host = new HostBuilder().UseServiceProviderFactory(new ServiceContainerFactory()) .ConfigureContainer ((hostContext, container) =>{ })
8.擴展性
在IHostBuilder上使用擴展方法實現主機擴展性。應用程序建立UseHostedService擴展方法,以注冊在T中傳遞的托管服務:
public static class Extensions { public static IHostBuilder UseHostedService(this IHostBuilder hostBuilder) where T : class, IHostedService, IDisposable { return hostBuilder.ConfigureServices(services => services.AddHostedService ()); } }
9.管理主機
IHost實現負責啟動和停止由服務容器中注冊的IHostedService實現。
9.1Run
Run運行應用程序并阻止調用線程,直到關閉主機:
public class Program { public void Main(string[] args) { var host = new HostBuilder().Build(); host.Run(); } }
9.2RunAsync
RunAsync運行應用程序并返回在觸發取消令牌或關閉時完成的Task:
public class Program { public static async Task Main(string[] args) { var host = new HostBuilder().Build(); await host.RunAsync(); } }
9.3RunConsoleAsync
RunConsoleAsync啟用控制臺、生成和啟動主機,以及等待Ctrl+C/SIGINT或SIGTERM關閉。
public class Program { public static async Task Main(string[] args) { var hostBuilder = new HostBuilder(); await hostBuilder.RunConsoleAsync(); } }
9.4Start和StopAsync
Start同步啟動主機。StopAsync嘗試在提供的超時時間內停止主機。
public class Program { public static async Task Main(string[] args) { var host = new HostBuilder().Build(); using (host) { host.Start(); await host.StopAsync(TimeSpan.FromSeconds(5)); } } }
9.5StartAsync和StopAsync
StartAsync啟動應用程序。StopAsync停止應用程序。
public class Program { public static async Task Main(string[] args) { var host = new HostBuilder().Build(); using (host) { await host.StartAsync(); await host.StopAsync(); } } }
9.6WaitForShutdown
WaitForShutdown通過IHostLifetime觸發,例如ConsoleLifetime(偵聽Ctrl+C/SIGINT或SIGTERM)。WaitForShutdown調用StopAsync。
public class Program { public void Main(string[] args) { var host = new HostBuilder().Build(); using (host) { host.Start(); host.WaitForShutdown(); } } }
9.7WaitForShutdownAsync
WaitForShutdownAsync返回在通過給定的令牌和調用StopAsync來觸發關閉時完成的Task。
public class Program { public static async Task Main(string[] args) { var host = new HostBuilder().Build(); using (host) { await host.StartAsync(); await host.WaitForShutdownAsync(); } } }
9.8External control(外部控件)
public class Program { private IHost _host; public Program() { _host = new HostBuilder() .Build(); } public async Task StartAsync() { _host.StartAsync(); } public async Task StopAsync() { using (_host) { await _host.StopAsync(TimeSpan.FromSeconds(5)); } } }
在StartAsync開始時調用WaitForStartAsync,在繼續之前,會一直等待該操作完成。它可用于延遲啟動,直到外部事件發出信號。
10.IHostingEnvironment、IApplicationLifetime接口
該兩個接口類型跟上一篇Web主機IHostingEnvironment、IApplicationLifetime接口類型是一樣的,詳情就不在這多講了,想要了解的請移步到上一篇Web主機文章。
原文鏈接:https://www.cnblogs.com/wzk153/p/11340588.html
相關推薦
- 2022-11-24 C++?OpenCV實現boxfilter方框濾波的方法詳解_C 語言
- 2022-12-05 python?argparse?模塊命令行參數用法及說明_python
- 2022-11-10 C語言結構體struct詳解_C 語言
- 2024-03-17 WSL子系統啟動報錯 Wsl/Service/CreateInstance/CreateVm/HCS
- 2022-10-03 Docker容器/bin/bash?start.sh無法找到not?found問題解決_docker
- 2022-08-06 Flutter?隊列任務的實現_Android
- 2022-09-05 Redis的數據刪除策略
- 2022-09-17 Pandas數據類型轉換df.astype()及數據類型查看df.dtypes的使用_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支