• asp.net core mvc 里的application中的start,end等事件


    我们以前在用asp.net mvc或者webform的时候,经常用用到Application里的事件 start,end等。我们在.net core 里也同样有类似的方法。

    在Startup类里,Configure方法里添加一个参数IHostApplicationLifetime  applicationLeftTime就可以了。具体写法如下:

    IHostApplicationLifetime 为.netcore 3.1的写法,如果为.netcore 2.*,则用IApplicationLifetime

      public void Configure(IApplicationBuilder app, IHostingEnvironment env,IHostApplicationLifetime applicationLeftTime)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                    app.UseHsts();
                }
    
                applicationLeftTime.ApplicationStarted.Register(() =>
                {
                    //里面可以写其他逻辑
    
                    Console.Write("ApplicationStarted");
                });
    
                applicationLeftTime.ApplicationStopped.Register(()=> {
                    //里面可以写其他逻辑
                    Console.Write("ApplicationStopped");
                });
    
                applicationLeftTime.ApplicationStopping.Register(() => {
                    //里面可以写其他逻辑
                    Console.Write("ApplicationStopping");
                });
    
    
    
                app.UseHttpsRedirection();
                app.UseStaticFiles();
                app.UseCookiePolicy();
    
                app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}");
                });
            }
  • 相关阅读:
    POJ 1141 括号匹配 DP
    881. Boats to Save People
    870. Advantage Shuffle
    874. Walking Robot Simulation
    文件操作
    861. Score After Flipping Matrix
    860. Lemonade Change
    842. Split Array into Fibonacci Sequence
    765. Couples Holding Hands
    763. Partition Labels
  • 原文地址:https://www.cnblogs.com/puzi0315/p/11318539.html
Copyright © 2020-2023  润新知