28

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"));
    }
wonea
  • 4,783
  • 17
  • 86
  • 139
James Ellis-Jones
  • 3,042
  • 21
  • 13
  • How can we tell if you're doing something wrong if you don't show us what you are doing? – DavidG Dec 21 '16 at 16:55
  • Good point, although it's fairly unremarkable code, I've added it now – James Ellis-Jones Dec 21 '16 at 17:45
  • May be a silly question, but you have installed the IIS hosting module, right? https://learn.microsoft.com/en-us/aspnet/core/publishing/iis#install-the-net-core-windows-server-hosting-bundle – Mitch Dec 21 '16 at 19:42
  • Other than that, make sure you also have the 1.1 runtime and SDK downloaded from the .NET Core downloads page. Apart from that, there's not much help we can give with the current info. Try the event viewer application log too. It's more likely to be configuration related rather than code related, so you're not going to be able to set a debug break point I'd that's what you're hoping to do. – Mitch Dec 21 '16 at 19:44
  • 1
    did you see this link https://shazwazza.com/post/aspnet-core-application-shutdown-events/ i think you lose the life for this it never run – Shahrooz Ansari May 06 '17 at 14:53
  • Did you try if Application_End in Global.aspx.cs meets your purpose? This method will be called before your application is unloaded. – Gururaj May 08 '17 at 18:16
  • @Gururaj I don't have a Global.aspx, it's a API project – wonea May 09 '17 at 08:03
  • WebApi Application is also derived from System.Web.HttpApplication. Look for "public class WebApiApplication : System.Web.HttpApplication" in your code and you should find it – Gururaj May 09 '17 at 12:20
  • System.Web.HttpApplication is only applicable in ASP.NET 4.x and below, this appears to be an ASP.NET Core project. There's no Global.asax file in ASP.NET Core. – Andrew Stanton-Nurse Aug 25 '17 at 18:38

2 Answers2

11

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.

wonea
  • 4,783
  • 17
  • 86
  • 139
fj42
  • 196
  • 2
  • 6
  • 2
    Yup. Ctrl+C only. It apparently will not fire when using the window's standard [X] to close console window. Seems like a bug to me, since I never had any huge issues with detecting console-closed event, at least on Windows platform... – quetzalcoatl Nov 03 '17 at 13:51
-4

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

Gururaj
  • 539
  • 2
  • 8