如何在AspNet5/Mvc6中检测dnx451 Web应用程序关闭? [英] How to detect dnx451 web application shutdown in AspNet5 / Mvc6?

查看:88
本文介绍了如何在AspNet5/Mvc6中检测dnx451 Web应用程序关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要能够关闭后台进程(通过Quartz.Net实现),我需要在AspNet5 beta8中检测Web应用程序的关闭.在以前的Asp.Net版本中,可以在Application_End上执行代码.什么是AspNet5中的Application_End事件?

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?

到目前为止,我在配置"中尝试了IApplicatonLifetime,但是在停止Web应用程序时不会触发:

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

在ApplicationStopping和ApplicationStopped上都没有得到任何回应.

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

推荐答案

我尝试使用旧版本(beta5),并且您的代码运行正常.使用IIS Express并正常停止网站时会触发ApplicationStopping和ApplicationStopped.

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.

在beta8更改之后,IIS中的这些事件似乎存在问题. Beta8 更改了IIS托管,因此它使用HttpPlatformHandler启动dnx进程.不幸的是,似乎没有收到关闭事件,请参见问题(没有太多内容)有关该问题的详细信息).

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).

我在计算机上尝试了beta8,问题似乎仅限于IIS:

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

  • 当我使用IIS Express时,仅触发ApplicationStarted事件(即使正常停止站点时也是如此).
  • 但是,如果我运行dnx web,则会触发3个事件.
  • 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.

可以使用dnx命令通过多种方式运行项目:

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

  • 是否要使用IIS Express或dnx命令运行项目,请告诉Visual Studio.运行按钮中有一个下拉菜单:

  • 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:

在项目根文件夹中打开一个命令窗口,然后运行dnx web(假设web是您project.json文件中配置的命令).

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

这篇关于如何在AspNet5/Mvc6中检测dnx451 Web应用程序关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆