Getting ASP.Net Core shutdown triggering ApplicationStopping event in IISExpress

5.9k views Asked by At

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"));
    }
2

There are 2 answers

1
Gururaj On

Maybe you can have Application_End method in the class deriving from HttpApplication. This method will be invoked when your application is being offloaded.

1
fj42 On

I've an application with all latest NuGet updates and can an verify the ApplicationStopped and ApplicationStopping are not triggered under IISExpress. ApplicationStarted seems to work okay.

I've found if you invoke the application directly in my dev environment all is fine.

This article tells you how.

It is a simple as selecting the app from the drop down box:

example

I realise this is not the same as using IIS Express, but might help you with your problem.

Nearly forgot, to invoke you need to type Ctrl+C in the command window which appears when you start your app.