简体   繁体   English

在ASP.Net Core 2.0中由于任何原因停止Kestrel时执行瓶坯清理

[英]Preform cleanup when Kestrel is stopped for any reason in ASP.Net Core 2.0

I'm working on a web application which has multiple threads running which do stuff with sockets and the file I/O. 我正在使用一个运行多个线程的Web应用程序,这些线程可以处理套接字和文件I / O。 So in the event of the application shutting down, I first want to finish these threads and clean them up. 因此,在应用程序关闭的情况下,我首先要完成这些线程并清理它们。

I already found the following code, which works great in the event of a gracefull shutdown. 我已经找到了以下代码,如果完全关闭,效果很好。

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

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

But when you have an ungraceful shutdown the OnShutDown() method is not called. 但是,当您进行不正常的关机时,不会调用OnShutDown()方法。

In Forms you can detect all kinds of shutdown reasons with the following code: 在Forms中,您可以使用以下代码检测各种关闭原因:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)  
{  
    switch (e.CloseReason)  
    {  
        case CloseReason.ApplicationExitCall:  
            // The Exit method of the Application class was called.  
            break;  
        case CloseReason.FormOwnerClosing:  
            // The owner form is closing.  
            break;  
        case CloseReason.MdiFormClosing:  
            // The parent form is closing.  
            break;  
        case CloseReason.None:  
            // Unknown closing reason.  
            break;  
        case CloseReason.TaskManagerClosing:  
            // The application is being closed from the TaskManager.  
            break;  
        case CloseReason.UserClosing:  
            // The user is closing the form through the UI.  
            break;  
        case CloseReason.WindowsShutDown:  
            // Windows is closing the application because it is shutting down.  
            break;  
    }  
} 

Is there anything like this in ASP.Net Core 2.0 to detect any kind of shutdown in stat of only graceful shutdowns? 在ASP.Net Core 2.0中是否有类似的功能可以检测仅正常关机状态的任何关机?

Event handling is predicated on a successful exit. 事件处理以成功退出为前提。 If you do something like a hard reboot or your application crashes, there's no opportunity to handle anything because it's already gone. 如果您执行诸如硬重启或应用程序崩溃之类的操作,则没有机会处理任何事情,因为它已经消失了。 That's sort of the point: it was forced to exit, so it can't do anything. 这就是重点:它被迫退出,因此它无能为力。

That said, the issue here is either not actually an issue or a design problem. 就是说,这里的问题实际上不是问题,也不是设计问题。 First, if the process exits, the threads go with it. 首先,如果进程退出,则线程随之进行。 There's nothing to "cleanup". 没有什么可以“清理”的。 Socket handles and such are just pointers in memory, they too go with the process. 套接字句柄等只是内存中的指针,它们也随进程一起使用。

If the process is hanging, that's a different story. 如果过程中止了,那就另当别论了。 In that situation, it can hold on to resources, since it's still technically running. 在那种情况下,它可以保留资源,因为它在技术上仍在运行。 However, then, the true issue is fixing whatever is causing the process to hang, not figuring out how to release resources when it does. 但是,真正的问题是要修复导致进程挂起的所有内容,而不是弄清楚如何在此过程中释放资源。

Bear in mind that a web server is designed to quickly respond to incoming requests. 请记住,Web服务器旨在快速响应传入的请求。 It should be an almost instantaneous process. 这应该是一个瞬时过程。 It's not designed to chew on requests for a while, doing a bunch of work. 它的设计目的不是要花一会儿时间来做大量的工作。 If you need to do things like connect to sockets and such, that should very likely be offloaded into an external process, like a console application, windows service, etc. Then, your web application can simply hand off work to this and move on. 如果您需要执行诸如连接套接字之类的操作,那么很可能应该将其卸载到外部进程中,例如控制台应用程序,Windows服务等。然后,您的Web应用程序可以简单地将工作移交给它并继续进行。 You can provide an endpoint to check for status, progress, etc., and use that via SignalR or AJAX long-polling to update the client about the progress of the work/notify them when it's done. 您可以提供一个端点来检查状态,进度等,并通过SignalR或AJAX长轮询使用该端点,以向客户端更新工作的进度/在完成时通知他们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM