• ASP.NETCORE 启动事件的两种捕获方式--可以自定义预加载和卸载相关任务


    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>();
                    });
        }
    }
    龙腾一族至尊龙骑
  • 相关阅读:
    Notepad++ 中如何将代码格式化
    JAVA 解析复杂的json字符串
    8. java操作mongodb——查询数据
    7.第一次使用java连接mongodb遇到的问题
    13. Intellij IDEA调试功能使用总结
    HttpClient4.5简单使用
    12.Intellij IDEA 添加jar包的三种方式
    11.IntelliJ IDEA详细配置和使用教程(适用于Java开发人员)
    10.Intellij IDEA svn的使用详解
    黑客攻克索尼影业,掌控了操作系统的未来
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/15039432.html
Copyright © 2020-2023  润新知