ASP.NET Core中Startup.cs中的Kestrel关闭功能 [英] Kestrel shutdown function in Startup.cs in ASP.NET Core

查看:296
本文介绍了ASP.NET Core中Startup.cs中的Kestrel关闭功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Microsoft.AspNet.Server.Kestrel时是否有关机功能? ASP.NET Core(以前为ASP.NET vNext)显然具有启动顺序,但没有提及关闭顺序以及如何处理干净关闭.

Is there a shutdown function when using Microsoft.AspNet.Server.Kestrel? ASP.NET Core (formerly ASP.NET vNext) clearly has a Startup sequence, but no mention of shutdown sequence and how to handle clean closure.

推荐答案

在ASP.NET Core中,您可以注册到IApplicationLifetime

In ASP.NET Core you can register to the cancellation tokens provided by IApplicationLifetime

public class Startup 
{
    public void Configure(IApplicationBuilder app, IApplicationLifetime applicationLifetime) 
    {
        applicationLifetime.ApplicationStopping.Register(OnShutdown);
    }

    private void OnShutdown()
    {
         // Do your cleanup here
    }
}

IApplicationLifetime还公开了ApplicationStoppedApplicationStarted的取消标记以及StopApplication()方法来停止应用程序.

IApplicationLifetime is also exposing cancellation tokens for ApplicationStopped and ApplicationStarted as well as a StopApplication() method to stop the application.

来自评论 @Horkrine

对于.NET Core 3.0+,建议改用IHostApplicationLifetime,因为IApplicationLifetime将很快被弃用.其余部分仍将按照新服务的上述说明工作

For .NET Core 3.0+ it is recommended to use IHostApplicationLifetime instead, as IApplicationLifetime will be deprecated soon. The rest will still work as written above with the new service

这篇关于ASP.NET Core中Startup.cs中的Kestrel关闭功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆