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

别等时光非礼了梦想. 提交于 2019-12-10 15:59:37

问题


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.


回答1:


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:

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



来源:https://stackoverflow.com/questions/33440875/how-to-detect-dnx451-web-application-shutdown-in-aspnet5-mvc6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!