2013-07-17 32 views
2

我有一個從客戶端收集使用情況數據的Web服務器集羣(Windows 2008)。這些服務器都會批量處理數據並在一定時間或數據量之後發送。問題是我們正在使用AWS自動縮放,並且可以隨時關閉機器。我想在應用程序被終止之前檢測到關閉事件並將使用情況數據發送到數據庫。有誰知道這是否可能?如何在MVC .NET中關閉執行之前檢測關閉並運行代碼

回答

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

+0

服務器不回去了,當他們關閉,他們都將終止。我試圖關閉網站在批處理文件,如你所建議的,但我遇到時間問題http://stackoverflow.com/questions/17733989/halt-batch-file-until-website-stop-is - 完整 – JoshNaro

+1

檢查此線程。 http://stackoverflow.com/questions/101647/how-to-schedule-a-task-to-run-when-shutting-down-windows –

1

您可以訂閱這個事件之前應用程序關閉執行你的邏輯。

Application.Current.Exit + = DoSomething;

+0

試過。它適用於手動停止網站,但不在Windows關閉時。正如T.S.鏈接所建議的,可能需要修改Windows關機策略,以便在關機期間停止站點。 – JoshNaro

0

WebActivator是NuGet包,旨在清理應用程序StartShutdown註冊。

這將註冊您的MyStaticClass.Start()MyStaticClass.Shutdown()方法:

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