How to detect dnx451 web application shutdown in AspNet5 / Mvc6?
Asked Answered
J

1

4

To be able to shutdown background process (implemented with Quartz.Net) I need to detect web application shutdown in AspNet5 beta8. In previous versions of Asp.Net it was possible to execute code on Application_End. What is the equivalent of Application_End event in AspNet5?

So far I tried IApplicatonLifetime in Configure, but it does not fire when I stop the web application:

public void Configure(IApplicationBuilder app, IApplicationLifetime lifetime)
{
    lifetime.ApplicationStopping.Register(() =>
    {
        Logger.LogInformation("Application Stopping. Do stuff.");
    });

    lifetime.ApplicationStopped.Register(() =>
    {        
        Logger.LogInformation("Application Stopped. Do stuff.");
    });
}

I don't get any response on both ApplicationStopping and ApplicationStopped.

Jhansi answered 30/10, 2015 at 16:40 Comment(0)
B
4

I have tried an older version (beta5) and your code worked fine. The ApplicationStopping and ApplicationStopped where fired when using IIS Express and gracefully stopping the site.

It seems that there is an issue with these events in IIS after the beta8 changes. Beta8 changed IIS hosting so it uses the HttpPlatformHandler to launch the dnx process. Unfortunately it seems that shutdown events are not received, see the issue (There are no much details regarding the issue as of today).

I have tried beta8 on my machine and the issue seems limited to IIS:

  • When I use IIS Express only the ApplicationStarted event is fired (Even when gracefully stopping the site).
  • However if I run dnx web then the 3 events are fired.

There are a couple of ways you can run the project using the dnx command:

  • Tell Visual Studio whether you want to run the project using IIS Express or the dnx command. There is a dropdown menu in the run button:

    Choosing web or IISExpress when running project

  • Open a command window in your project root folder and run dnx web (assuming web is the command configured in your project.json file).

Barcarole answered 31/10, 2015 at 21:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.