deandob
deandob

Reputation: 5280

Capture shutdown command for graceful close in .NET Core

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.

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.

The other option is via AppDomain (AppDomain.CurrentDomain.ProcessExit) however AppDomain isn't available in dotnet Core.

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.

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?

Upvotes: 14

Views: 6331

Answers (3)

Tim Cooper
Tim Cooper

Reputation: 10468

These days I think the recommended way is using 'Program.cs'. If you look at your 'Program.cs' you'll probably see:

app.Run();

at the bottom. You can add shutdown code below that, e.g. a call to a static function on one of your own classes.

To test that in Visual Studio, you obviously can't use the red stop button. Instead, go to the Windows system tray, "Show hidden icons", find the IIS icon, and click it to shutdown.

Upvotes: 1

alastairtree
alastairtree

Reputation: 4289

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.

More info at 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.

Upvotes: 4

Marc Sigrist
Marc Sigrist

Reputation: 4190

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.

Upvotes: 4

Related Questions