【问题标题】:How to detect a shutdown and run code before shutdown execution in MVC .NET如何在 MVC .NET 中关闭执行之前检测关闭并运行代码
【发布时间】:2013-07-17 15:21:35
【问题描述】:

我有一个从客户端收集使用数据的 Web 服务器集群(Windows 2008)。服务器都对数据进行批处理,并在一定时间或一定数量的数据后发送。问题是我们使用的是 AWS 自动缩放,机器可以随时关闭。我想在应用程序被杀死之前检测关闭事件并将使用数据发送到数据库。有谁知道这是否可能?

【问题讨论】:

    标签: asp.net-mvc windows events shutdown


    【解决方案1】:

    我想说,当您的服务器恢复正常时,您可以执行这些任务(在代码中/您自己)。

    否则看这里http://social.technet.microsoft.com/Forums/windowsserver/en-US/bd8ea190-9bf4-4915-8ed9-96ee5d6f336a/when-are-windows-server-20032008-shutdown-scripts-run-in-the-shutdown-process

    【讨论】:

    【解决方案2】:

    您可以订阅此事件并在应用程序关闭之前执行您的逻辑。

    Application.Current.Exit +=DoSomething;

    【讨论】:

    • 试过了。如果手动停止网站,它可以工作,但在 Windows 关闭时不起作用。可能需要修改 Windows 关闭策略,以便在关闭期间停止站点,如 T.S. 的链接所建议的那样。
    【解决方案3】:

    WebActivator 是 NuGet 包,旨在清理应用程序 Start 和 Shutdown 注册。

    这将注册您的 MyStaticClass.Start() 和 MyStaticClass.Shutdown() 方法:

    [assembly: WebActivator.PostApplicationStartMethod(typeof(MyStaticClass), "Start")]
    [assembly: WebActivator.ApplicationShutdownMethod(typeof(MyStaticClass), "Shutdown")]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多