I'm porting to .NET core and the existing app has hooked the win32 call SetConsoleCtrlHandler to capture application close so that it can cleanly close off open database files and other orderly shutdown tasks.
我正在移植到.NET核心,现有的应用程序已经挂钩win32调用SetConsoleCtrlHandler来捕获应用程序关闭,以便它可以干净地关闭打开的数据库文件和其他有序的关闭任务。
I need to do the same in .NET core, and the only option I can see is with ASP.NET by hooking into IApplicationLifetime ApplicationStopping.Register. However that only captures Ctrl-C and does not work if the end user or system terminates the console app (eg. system reboot) which the win32 call will.
我需要在.NET核心中做同样的事情,我能看到的唯一选择是通过挂钩到IApplicationLifetime ApplicationStopping.Register来使用ASP.NET。但是,如果最终用户或系统终止win32呼叫将要使用的控制台应用程序(例如,系统重启),则仅捕获Ctrl-C并且不起作用。
The other option is via AppDomain (AppDomain.CurrentDomain.ProcessExit) however AppDomain isn't available in dotnet Core.
另一个选项是通过AppDomain(AppDomain.CurrentDomain.ProcessExit),但是在dotnet Core中没有AppDomain。
Orderly app shutdown is important and I'm concerned about data corruption in the production system so need to implement something equivalent, and the new app will be running on a mix of Windows and Linux OS.
有序的应用程序关闭很重要,我担心生产系统中的数据损坏,因此需要实现等效的东西,新的应用程序将在Windows和Linux操作系统的混合上运行。
Have I missed a function in dotnet core to do this? Or do I have to re-implement the win32 shutdown hook and whatever is the equivalent on Linux?
我是否错过了dotnet核心中的功能?或者我是否必须重新实现win32关闭钩子以及Linux上的等效物?
2 个解决方案
#1
2
The AppDomain.CurrentDomain.ProcessExit
event is now available in .NET Core 2.0, and I can confirm that it works fine on Linux. Before .NET Core 2.0, AssemblyLoadContext.Default.Unloading
is probably a working alternative.
AppDomain.CurrentDomain.ProcessExit事件现在可以在.NET Core 2.0中使用,我可以确认它在Linux上运行正常。在.NET Core 2.0之前,AssemblyLoadContext.Default.Unloading可能是一个可行的替代方案。
#2
2
If you can update to dotnet core 2.1 (or later) you should consider using the IHost
interface for console apps and IHostedService
for asp.net core apps (2.0 and upwards) which are designed for just this sort of thing - telling the framework you have background processes that need to be notified on shutdown.
如果您可以更新到dotnet core 2.1(或更高版本),您应该考虑使用IHost接口用于控制台应用程序和IHostedService用于asp.net核心应用程序(2.0及更高版本),这些应用程序专为此类事物而设计 - 告诉您拥有的框架需要在关机时通知的后台进程。
更多信息,请访问https://blogs.msdn.microsoft.com/cesardelatorre/2017/11/18/implementing-background-tasks-in-microservices-with-ihostedservice-and-the-backgroundservice-class-net-core-2 -X/
Admittedly you may need AppDomain.CurrentDomain.ProcessExit
as well for the killed process scenario, but using IHost/IHostedService will give you more time for a graceful shutdown in most application shutdowns.
不可否认,您可能还需要AppDomain.CurrentDomain.ProcessExit来处理被杀死的进程场景,但是使用IHost / IHostedService可以让您有更多时间在大多数应用程序关闭时正常关闭。
#1
2
The AppDomain.CurrentDomain.ProcessExit
event is now available in .NET Core 2.0, and I can confirm that it works fine on Linux. Before .NET Core 2.0, AssemblyLoadContext.Default.Unloading
is probably a working alternative.
AppDomain.CurrentDomain.ProcessExit事件现在可以在.NET Core 2.0中使用,我可以确认它在Linux上运行正常。在.NET Core 2.0之前,AssemblyLoadContext.Default.Unloading可能是一个可行的替代方案。
#2
2
If you can update to dotnet core 2.1 (or later) you should consider using the IHost
interface for console apps and IHostedService
for asp.net core apps (2.0 and upwards) which are designed for just this sort of thing - telling the framework you have background processes that need to be notified on shutdown.
如果您可以更新到dotnet core 2.1(或更高版本),您应该考虑使用IHost接口用于控制台应用程序和IHostedService用于asp.net核心应用程序(2.0及更高版本),这些应用程序专为此类事物而设计 - 告诉您拥有的框架需要在关机时通知的后台进程。
更多信息,请访问https://blogs.msdn.microsoft.com/cesardelatorre/2017/11/18/implementing-background-tasks-in-microservices-with-ihostedservice-and-the-backgroundservice-class-net-core-2 -X/
Admittedly you may need AppDomain.CurrentDomain.ProcessExit
as well for the killed process scenario, but using IHost/IHostedService will give you more time for a graceful shutdown in most application shutdowns.
不可否认,您可能还需要AppDomain.CurrentDomain.ProcessExit来处理被杀死的进程场景,但是使用IHost / IHostedService可以让您有更多时间在大多数应用程序关闭时正常关闭。