如何在 AspNet5 / Mvc6 中检测 dnx451 Web 应用程序关闭?

2024-03-03

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

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

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 的任何响应。


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

beta8 更改后,IIS 中的这些事件似乎存在问题。贝塔8更改了 IIS 托管 https://github.com/aspnet/Announcements/issues/69因此它使用 HttpPlatformHandler 来启动 dnx 进程。不幸的是,似乎没有收到关闭事件,请参阅问题 https://github.com/aspnet/IISIntegration/issues/20(截至今天还没有关于该问题的更多细节)。

我在我的机器上尝试过 beta8,问题似乎仅限于 IIS:

  • 当我仅使用 IIS Express 时ApplicationStarted事件被触发(即使正常停止站点)。
  • 但是如果我跑dnx web然后这 3 个事件被触发。

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

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

  • 在项目根文件夹中打开命令窗口并运行dnx web(假设 web 是在 project.json 文件中配置的命令)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 AspNet5 / Mvc6 中检测 dnx451 Web 应用程序关闭? 的相关文章

随机推荐