.Net Core 通用主机(Core 在控制台应用程序中的应用)
一、介绍
官方文档中说,Microsoft.AspNetCore.App 元包(ASP.NET Core 2.1 或更高版本)包含通用主机的Microsoft.Extensions.Hosting包,当创建控制台项目以后并没有相应的包。
在官方案例中所用的Nuget包有:
1. Microsoft.Extensions.Hosting
2. Microsoft.Extensions.Configuration.Json
3. Microsoft.Extensions.Configuration.EnvironmentVariables
4. Microsoft.Extensions.Configuration.CommandLine
5. Microsoft.Extensions.DependencyInjection
6. Microsoft.Extensions.Logging.Console
7. Microsoft.Extensions.Logging.Debug
托管服务实现 IHostedService 接口并且是执行代码的入口点。 每个 IHostedService 实现都按照 ConfigureServices 中服务注册的顺序执行。 主机启动时,每个 IHostedService 上都会调用 StartAsync。主机正常关闭时,以反向注册顺序调用 StopAsync。
托管服务还有,BackgroundService:排队的后台任务,IScopedProcessingService:有作用域的服务
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using System; using System.IO; namespace Haos.Develop.HostBuilders.Samples { class Program { static void Main(string[] args) { //Microsoft.Extensions.Hosting var host = new HostBuilder() .ConfigureHostConfiguration(builder => { //Microsoft.Extensions.Configuration.Json builder.AddJsonFile("hostsettings.json", true); builder.AddJsonFile("appsettings.json", true); }) .ConfigureAppConfiguration((context, builder) => { var temp = context.Configuration["AllowedHosts"]; context.HostingEnvironment.ApplicationName = "this_is_text_host"; builder.SetBasePath(Directory.GetCurrentDirectory()); //Microsoft.Extensions.Configuration.EnvironmentVariables builder.AddEnvironmentVariables("PREFIX_HAOS_"); //Microsoft.Extensions.Configuration.CommandLine builder.AddCommandLine(args); }) .ConfigureServices((context, services) => { services.AddHostedService<MyHostedService>(); }) .ConfigureLogging((logging) => { //Microsoft.Extensions.Logging.Console logging.AddConsole(); //Microsoft.Extensions.Logging.Debug logging.AddDebug(); }) .UseConsoleLifetime() .Build(); host.Run(); } } }
二、主机配置
默认的情况下,是不配置主机环境变量。需要配置可以调用ConfigureHostConfiguration和ConfigureAppConfiguration方法进行配置,并且他们可以同时调用多次得到累计结果
var host = new HostBuilder() .ConfigureHostConfiguration(builder => { builder.AddJsonFile("hostsettings.json", true); }) .ConfigureHostConfiguration(builder => { builder.AddJsonFile("appsettings.json", true); }) .ConfigureAppConfiguration((context, builder) => {//应用入口点的程序集的名称 context.HostingEnvironment.ApplicationName = "this_is_text_host"; //确定主机从哪里开始搜索内容文件 builder.SetBasePath(Directory.GetCurrentDirectory()); //设置应用的环境 builder.AddEnvironmentVariables("PREFIX_HAOS_"); })
代码中调用两次ConfigureHostConfiguration方法都是加载配置文件。加载的文件在当前方法无法获取到文件的类容。例如第一次调用加载hostsettings.json文件无法立刻获取改文件内容。在加载appsettings.json这个方法里就能拿到hostsettings.json的内容
三、依赖关系注入,配置日志
ConfigureServices:将服务添加到应用的依赖关系注入容器。 可多次调用 ConfigureServices,并得到累计结果。
ConfigureLogging:添加一个委托,用于配置提供的 ILoggingBuilder。 可以利用相加结果多次调用 ConfigureLogging
var host = new HostBuilder() .ConfigureServices((context, services) => { //Microsoft.Extensions.DependencyInjection services.AddHostedService<MyHostedService>(); }) .ConfigureLogging((logging) => { //Microsoft.Extensions.Logging.Console logging.AddConsole(); //Microsoft.Extensions.Logging.Debug logging.AddDebug(); })
四、IApplicationLifetime接口和IHostedService接口的实现类
IApplicationLifetime 允许启动后和关闭活动,包括正常关闭请求。通过构造函数将 IApplicationLifetime 服务注入到任何类中,用于注册事件
ApplicationStarted:完全启动触发,ApplicationStopped:正在完成关闭触发,ApplicationStopping:正在执行关闭触发
StopApplication() 方法用于关闭整个主机
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Text; using System.Threading; using System.Threading.Tasks; namespace Haos.Develop.HostBuilders.Samples { public class MyHostedService : IHostedService { /// <summary> /// 日志 /// </summary> private readonly ILogger _looger; /// <summary> /// 配置 /// </summary> private readonly IConfiguration _configuration; private readonly IApplicationLifetime _applicationLifetime; public MyHostedService(ILogger<MyHostedService> logger,IConfiguration configuration,IApplicationLifetime applicationLifetime) { _looger = logger; _configuration = configuration; _applicationLifetime = applicationLifetime; } public Task StartAsync(CancellationToken cancellationToken) { return Task.Run(() => { var stringStr = _configuration["Test_Code"]; //主机已完全启动 _applicationLifetime.ApplicationStarted.Register(() => _looger.LogInformation("status is started")); //主机正在完成正常关闭,应处理所有请求 _applicationLifetime.ApplicationStopped.Register(()=> _looger.LogInformation(stringStr)); //主机正在执行正常关闭,仍在处理请求 _applicationLifetime.ApplicationStopping.Register(() => _looger.LogInformation("status is Stopping")); int i = 1; Timer timer = new Timer(a => { if (i == 2) Close(); i++; _looger.LogWarning("测试输出!"); }, null, 3000, 5000); }); } public Task StopAsync(CancellationToken cancellationToken) { return Task.Run(() => { _looger.LogInformation("Application Shutdown"); Console.Read(); }); } /// <summary> /// 请求应用终止.(整个应用) /// </summary> public void Close() { _applicationLifetime.StopApplication(); } } }