cs中的Kestrel关闭功能

cs中的Kestrel关闭功能

本文介绍了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

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

07-25 01:43