Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting ASP.Net Core shutdown triggering ApplicationStopping event in IISExpress

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"));
    }
like image 800
James Ellis-Jones Avatar asked Dec 21 '16 16:12

James Ellis-Jones


1 Answers

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.

like image 148
fj42 Avatar answered Oct 21 '22 16:10

fj42