1、新建项目ASP.NETCORE项目ASP.NETCORETest
2、在Startup.cs中添加如下,包括2种方式捕获启动和停止事件接口,其中FirstStartService : IHostedService ,SecondStartService : IHostedService,IHostedService是Microsoft.Extensions.Hosting下的IHostedService,该接口包括StartAsync,StopAsync,主应用程序启动和停止的接口
第一种方式如下
public void ConfigureServices(IServiceCollection services) { services.AddRazorPages(); //应用程序启动起处理的的任务 services.AddHostedService<FirstStartService>(); services.AddHostedService<SecondStartService>(); }
FirstStartService代码
using Microsoft.Extensions.Hosting; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace ASP.NETCORETest { public class FirstStartService : IHostedService { public async Task StartAsync(CancellationToken cancellationToken) { await Task.Run(() => { Console.WriteLine("FirstStartService......StartAsync"); }, cancellationToken); } public async Task StopAsync(CancellationToken cancellationToken) { await Task.Run(() => { Console.WriteLine("FirstStartService......StopAsync"); }, cancellationToken); } } }
SecondStartService代码
using Microsoft.Extensions.Hosting; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace ASP.NETCORETest { public class SecondStartService : IHostedService { public async Task StartAsync(CancellationToken cancellationToken) { await Task.Run(() => { Console.WriteLine("SecondStartService......"); }, cancellationToken); } public async Task StopAsync(CancellationToken cancellationToken) { await Task.Run(() => { Console.WriteLine("SecondStartService......StopAsync"); }, cancellationToken); } } }
第二种方式是使用IHostApplicationLifetime,该接口可以捕获lHost的启动和停事件,
第二种方式如下
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime) { //IHostApplicationLifetime 除了程序进入点外,Host的启动和停止,ASP.NET Core不像ASP.NET MVC用继承的方式捕捉启动及停止事件, //而是透过Startup.Configure注入IHostApplicationLifetime来补捉Application启动停止事件 lifetime.ApplicationStarted.Register(async () => await Task.Run(() => Console.WriteLine("IHostApplicationLifetime......Started"))); lifetime.ApplicationStopping.Register(async () => await Task.Run(() => Console.WriteLine("IHostApplicationLifetime......Stopping"))); lifetime.ApplicationStopped.Register(async () => await Task.Run(() => Console.WriteLine("IHostApplicationLifetime......Stopped"))); var tt = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(a => { System.Threading.Thread.Sleep(TimeSpan.FromSeconds(10)); Console.WriteLine("IHostApplicationLifetime......Stopp---------ing"); lifetime.StopApplication(); })) ; tt.Start(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); } app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); }); }
Program.cs中主方法如下:
using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace ASP.NETCORETest { public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Build().Run(); } public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); }); } }