For some reason, I treat a .NET 8 webapi as an application on Windows, and I publish it using a self-contained method and execute it.
But I want to know how to capture the event when the user closes the program via the icon on the Windows toolbar or the window’s upper-right corner.
Currently, I’m trying to use app.Lifetime.ApplicationStopping
, but it seems to only capture the termination triggered by pressing Ctrl+C
in the console, and cannot detect the closure triggered by clicking the icon or the upper right corner of the window.
In fact, my ultimate goal is to perform some actions when a particular service ends. It’s another .NET 8 DLL, but its destructors don’t seem to be triggered either.
So if there’s a better way to achieve this, please suggest it. Thanks.