I'm aware there is a previous question on this, also there is a GitHub issue: https://github.com/aspnet/Hosting/issues/846 which appears to be resolved as of Microsoft.AspNetCore.Server.IISIntegration 1.1. However despite having this version, this still doesn't appear to work in IISExpress (I'm testing it by having it do a Debug.WriteLine and also writing to a log file on ApplicationStopping and ApplicationStopped). I'm shutting down IISExpress using the tool tray widget.
I'm not sure whether I'm doing something wrong, whether IISExpress shut down in this way is supported as a 'graceful shutdown' which triggers these events. It looks like this may well work in IIS but you can't develop locally with ASP.Net Core and full IIS apparently, so I wonder if there is any way to trigger these events in a dev environment for testing?
Here's the code in Startup.cs:
public void Configure(IApplicationBuilder app, IApplicationLifetime life, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// other configure code here
life.ApplicationStopping.Register(() =>
Debug.WriteLine(">>Stopping"));
life.ApplicationStopped.Register(() =>
Debug.WriteLine(">>Stopped"));
}
Maybe you can have Application_End method in the class deriving from HttpApplication. This method will be invoked when your application is being offloaded.