Kestrel shutdown function in Startup.cs in ASP.NET Core

前端 未结 4 956
既然无缘
既然无缘 2020-11-29 07:07

Is there a shutdown function when using Microsoft.AspNet.Server.Kestrel? ASP.NET Core (formerly ASP.NET vNext) clearly has a Startup s

4条回答
  •  自闭症患者
    2020-11-29 08:00

    In addition to the original answer, I had an error while trying to wire the IApplicationLifetime within the constructor.

    I solved this by doing:

    public class Startup 
    {
        public void Configure(IApplicationBuilder app) 
        {
            var applicationLifetime = app.ApplicationServices.GetRequiredService();
            applicationLifetime.ApplicationStopping.Register(OnShutdown);
        }
    
        private void OnShutdown()
        {
             // Do your cleanup here
        }
    }
    

提交回复
热议问题