如何从Azure Web角色发生503时以编程方式重新启动应用程序池

时间:2012-11-21 13:12:48

标签: asp.net iis azure azure-web-roles

如果Azure站点的应用程序池因快速失败保护而关闭,是否可以自动重新启动它?

这个问题提出了几乎相同的问题,但与azure无关 ASP.NET application pool shutdown problem

可能使用WebRole来监控和修改此页面上的代码Is it possible to restart IIS on a Azure web role without restarting the process?

var mgr = new ServerManager();
var azurePools = mgr.ApplicationPools.Where(p => Guid.TryParse(p.Name));
azurePools.ToList().ForEach(p => p.Recycle());

1 个答案:

答案 0 :(得分:2)

您可以从启动任务运行the following script(确保创建提升的后台任务):

Timeout= 30000
set events = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecNotificationQuery("select * from __instancecreationevent where targetinstance isa 'Win32_NTLogEvent' and TargetInstance.LogFile='System' and TargetInstance.EventCode=5002") 
Do
    WScript.Echo "==========================================================================="
    WScript.Echo "Listening for IIS Rapid Fail Protection Events"
    Set objLatestEvent = events.NextEvent
    Wscript.Echo objLatestEvent.TargetInstance.Message
    ' get the AppPool name from the Eventlog message
    appPool = objLatestEvent.TargetInstance.InsertionStrings(0)
    WScript.Echo "Restarting Application Pool '" & appPool & "' in " & Timeout & " milliseconds"
    WScript.Sleep(Timeout)
    'construct ADSI path to failed AppPool and start by setting AppPoolCommand to 1
    set pool = GetObject("IIS://localhost/w3svc/AppPools/" & appPool)
    pool.AppPoolCommand = 1
    pool.SetInfo
    WScript.Echo "AppPool " & appPool & " restarted"
    WScript.Echo "==========================================================================="
    WScript.Echo
Loop

使用WMI,它将侦听IIS RFP事件。这是通过将ExecNotificationQueryNextEvent相结合来完成的。对NextEvent的调用将阻止,直到新事件到来。发生这种情况时,脚本将等待30秒并重新启动应用程序池。

无论如何,如果RFP启动,可能更适合了解您的流程为何一次又一次地崩溃。