共用方式為


應用程式集 < 區失敗的失敗設定>

概觀

集合 <failure><applicationPools> 專案的專案 <add> 會設定應用程式集區失敗時要採取的動作。 針對應用程式進行疑難排解或偵錯時,元素的 <failure>autoShutdownExeorphanActionExerapidFailProtection屬性特別有用,因為它們可讓您彈性地指定 Internet Information Services (IIS) 在應用程式失敗時所採取的動作,例如執行外部可執行檔來記錄或偵錯失敗。

相容性

版本 備註
IIS 10.0 未在 IIS 10.0 中修改專案 <failure>
IIS 8.5 未在 IIS 8.5 中修改專案 <failure>
IIS 8.0 未在 IIS 8.0 中修改專案 <failure>
IIS 7.5 未在 IIS 7.5 中修改專案 <failure>
IIS 7.0 元素 <failure> 是在 IIS 7.0 中引進。
IIS 6.0 元素 <failure> 會取代 IIS 6.0 IIsApplicationPools Metabase 屬性的部分。

安裝程式

<applicationPools>集合包含在 IIS 7 的預設安裝中。

作法

如何編輯快速失敗的保護組態設定

  1. 開啟 [Internet Information Services (IIS) 管理員

    • 如果您使用 Windows Server 2012 或 Windows Server 2012 R2:

      • 在工作列上,依序按一下 [伺服器管理員]、[工具],然後按一下 [Internet Information Services (IIS) Manager]。
    • 如果您使用 Windows 8 或 Windows 8.1:

      • 按住Windows鍵,按字母X,然後按一下[主控台]。
      • 按一下 [系統管理工具],然後按兩下 [ Internet Information Services (IIS) Manager]。
    • 如果您使用 Windows Server 2008 或 Windows Server 2008 R2:

      • 在工作列上,按一下 [ 開始],指向 [ 系統管理工具],然後按一下 [ Internet Information Services (IIS) 管理員]。
    • 如果您使用 Windows Vista 或 Windows 7:

      • 在工作列上,按一下 [開始],然後按一下[主控台]。
      • 按兩下 [系統管理工具],然後按兩下 [ Internet Information Services] (IIS) Manager
  2. 在 [ 連線] 窗格中,展開伺服器名稱,按一下 [ 應用程式集區],然後按一下您要編輯的應用程式集區。
    [連線] 窗格的影像,其中已醒目提示 [應用程式預設集區] 選項。

  3. 在 [ 動作 ] 窗格中,按一下 [ 進階設定...]

  4. 在 [ 進階設定 ] 對話方塊中,按一下您要編輯的快速失敗保護屬性,然後在對話方塊的 [屬性值] 區段中編輯值,然後按一下 [ 確定]。 例如,將 [失敗間隔] (分鐘) 變更為 4 ,並將 [失敗上限] 變更為 4
    [進階設定] 對話方塊的螢幕擷取畫面,其中已醒目提示 [失敗上限]。

組態

屬性

屬性 描述
autoShutdownExe 選擇性字串屬性。 指定要在 WWW 服務關閉應用程式集區時執行的可執行檔。 您可以使用 autoShutdownParams 屬性將參數傳送至可執行檔。
autoShutdownParams 選擇性字串屬性。 為 autoShutdownExe 屬性中指定的可執行檔指定命令列參數。
loadBalancerCapabilities 選擇性列舉屬性。 指定無法啟動背景工作進程時的行為,例如當要求佇列已滿或應用程式集區處於快速失敗保護時。 loadBalancerCapabilities屬性可以是下列其中一個可能的值。 預設值是 HttpLevel

描述
HttpLevel 指定傳回 503 錯誤碼。 非 HTTP 感知的負載平衡器不會切換至不同的節點,因為 503 回應會告知負載平衡器電腦狀況良好。

數值為 2

TcpLevel 指定 TCP 連線將會終止。 當您使用非 HTTP 感知的負載平衡器時,這會很有用。

數值為 1

orphanActionExe 選擇性字串屬性。 如果 orphanWorkerProcess 屬性設定為 true) ,則指定當 WWW 服務孤立背景工作進程 (時要執行的可執行檔。 您可以使用 orphanActionParams 屬性將參數傳送至可執行檔。
orphanActionParams 選擇性字串屬性。 指出 orphanActionExe 屬性所命名之可執行檔的命令列參數。 若要指定孤立進程的進程識別碼,請使用 %1%。
orphanWorkerProcess 選擇性的 Boolean 屬性。 指定是否要將背景工作進程指派給孤立狀態,而不是在應用程式集區失敗時終止它。

預設值是 false
rapidFailProtection 選擇性的 Boolean 屬性。 設定為 true 會指示 WWW 服務在下列情況下從應用程式集區中的所有應用程式中移除:

  • 背景工作進程當機的數目已達到 rapidFailProtectionMaxCrashes 屬性中指定的最大值。
  • 當機會在 rapidFailProtectionInterval 屬性中指定的分鐘數內發生。
預設值是 true
rapidFailProtectionInterval 選擇性的 timeSpan 屬性。 指定重設進程失敗計數之前的分鐘數。

預設值 (00:05:00 五分鐘) 。
rapidFailProtectionMaxCrashes 選擇性 uint 屬性。 指定 rapidFailProtectionInterval 屬性所指定分鐘數內允許的失敗數目上限。 注意: 從 IIS 7.5 開始,此值必須介於 1 到 2147483647之間。

預設值是 5

子元素

無。

組態範例

下列設定範例會針對名為 DefaultAppPool 的單一應用程式集區啟用快速失敗保護、設定快速失敗保護間隔 5 分鐘,並將分鐘數內的失敗數目上限設定為 5。

<applicationPools>
   <add name="DefaultAppPool">
     <failure rapidFailProtection="true"
        rapidFailProtectionInterval="00:05:00"
        rapidFailProtectionMaxCrashes="5" />
   </add>
   <applicationPoolDefaults>
     <processModel identityType="NetworkService" />
   </applicationPoolDefaults>
</applicationPools>

範例程式碼

下列程式碼範例會在您的伺服器上啟用 DefaultAppPool 的快速失敗保護,然後將保護間隔設定為五分鐘,並將損毀數目上限設定為 5。

AppCmd.exe

appcmd.exe set config -section:system.applicationHost/applicationPools /[name='DefaultAppPool'].failure.rapidFailProtection:"True" /commit:apphost

appcmd.exe set config -section:system.applicationHost/applicationPools /[name='DefaultAppPool'].failure.rapidFailProtectionInterval:"00:05:00" /commit:apphost

appcmd.exe set config -section:system.applicationHost/applicationPools /[name='DefaultAppPool'].failure.rapidFailProtectionMaxCrashes:"5" /commit:apphost

注意

當您使用 AppCmd.exe 來設定這些設定時,請務必將 認可 參數 apphost 設定為 。 這會將組態設定認可至ApplicationHost.config檔案中的適當位置區段。

C#

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{

   private static void Main()
   {

      using (ServerManager serverManager = new ServerManager())
      {
         Configuration config = serverManager.GetApplicationHostConfiguration();
         ConfigurationSection applicationPoolsSection = config.GetSection("system.applicationHost/applicationPools");
         ConfigurationElementCollection applicationPoolsCollection = applicationPoolsSection.GetCollection();
         ConfigurationElement addElement = FindElement(applicationPoolsCollection, "add", "name", @"DefaultAppPool");
         if (addElement == null) throw new InvalidOperationException("Element not found!");

         ConfigurationElement failureElement = addElement.GetChildElement("failure");
         failureElement["rapidFailProtection"] = true;
         failureElement["rapidFailProtectionInterval"] = TimeSpan.Parse("00:05:00");
         failureElement["rapidFailProtectionMaxCrashes"] = 5;
         serverManager.CommitChanges();
      }
   }

   private static ConfigurationElement FindElement(ConfigurationElementCollection collection, string elementTagName, params string[] keyValues)
   {
      foreach (ConfigurationElement element in collection)
      {
         if (String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase))
         {
            bool matches = true;
            for (int i = 0; i < keyValues.Length; i += 2)
            {
               object o = element.GetAttributeValue(keyValues[i]);
               string value = null;
               if (o != null)
               {
                  value = o.ToString();
               }
               if (!String.Equals(value, keyValues[i + 1], StringComparison.OrdinalIgnoreCase))
               {
                  matches = false;
                  break;
               }
            }
            if (matches)
            {
               return element;
            }
         }
      }
      return null;
   }
}

VB.NET

Imports System
Imports System.Text
Imports Microsoft.Web.Administration

Module Sample
   Sub Main()
      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetApplicationHostConfiguration
      Dim applicationPoolsSection As ConfigurationSection = config.GetSection("system.applicationHost/applicationPools")
      Dim applicationPoolsCollection As ConfigurationElementCollection = applicationPoolsSection.GetCollection
      Dim addElement As ConfigurationElement = FindElement(applicationPoolsCollection, "add", "name", "DefaultAppPool")

      If (addElement Is Nothing) Then
         Throw New InvalidOperationException("Element not found!")
      End If

      Dim failureElement As ConfigurationElement = addElement.GetChildElement("failure")
      failureElement("rapidFailProtection") = True
      failureElement("rapidFailProtectionInterval") = TimeSpan.Parse("00:05:00")
      failureElement("rapidFailProtectionMaxCrashes") = 5

      serverManager.CommitChanges()
   End Sub

   Private Function FindElement(ByVal collection As ConfigurationElementCollection, ByVal elementTagName As String, ByVal ParamArray keyValues() As String) As ConfigurationElement
      For Each element As ConfigurationElement In collection
         If String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase) Then
            Dim matches As Boolean = True
            Dim i As Integer
            For i = 0 To keyValues.Length - 1 Step 2
               Dim o As Object = element.GetAttributeValue(keyValues(i))
               Dim value As String = Nothing
               If (Not (o) Is Nothing) Then
                  value = o.ToString
               End If
               If Not String.Equals(value, keyValues((i + 1)), StringComparison.OrdinalIgnoreCase) Then
                  matches = False
                  Exit For
               End If
            Next
            If matches Then
               Return element
            End If
         End If
      Next
      Return Nothing
   End Function


End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";

var applicationPoolsSection = adminManager.GetAdminSection("system.applicationHost/applicationPools", "MACHINE/WEBROOT/APPHOST");

var applicationPoolsCollection = applicationPoolsSection.Collection;

var addElementPos = FindElement(applicationPoolsCollection, "add", ["name", "DefaultAppPool"]);
if (addElementPos == -1) throw "Element not found!";
var addElement = applicationPoolsCollection.Item(addElementPos);

var failureElement = addElement.ChildElements.Item("failure");
failureElement.Properties.Item("rapidFailProtection").Value = true;
failureElement.Properties.Item("rapidFailProtectionInterval").Value = "00:05:00";
failureElement.Properties.Item("rapidFailProtectionMaxCrashes").Value = 5;

adminManager.CommitChanges();

function FindElement(collection, elementTagName, valuesToMatch) {
   for (var i = 0; i < collection.Count; i++) {
      var element = collection.Item(i);
      if (element.Name == elementTagName) {
         var matches = true;
         for (var iVal = 0; iVal < valuesToMatch.length; iVal += 2) {
            var property = element.GetPropertyByName(valuesToMatch[iVal]);
            var value = property.Value;
            if (value != null) {
               value = value.toString();
            }
            if (value != valuesToMatch[iVal + 1]) {
               matches = false;
               break;
            }
         }
         if (matches) {
            return i;
         }
      }
   }
   return -1;
}

VBScript

Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set applicationPoolsSection = adminManager.GetAdminSection("system.applicationHost/applicationPools", "MACHINE/WEBROOT/APPHOST")
Set applicationPoolsCollection = applicationPoolsSection.Collection
addElementPos = FindElement(applicationPoolsCollection, "add", Array("name", "DefaultAppPool"))

If (siteElementPos = -1) Then
   WScript.Echo "Element not found!"
   WScript.Quit
End If

Set addElement = applicationPoolsCollection.Item(addElementPos)
Set failureElement = addElement.ChildElements.Item("failure")
failureElement.Properties.Item("rapidFailProtection").Value = true
failureElement.Properties.Item("rapidFailProtectionInterval").Value = "00:05:00"
failureElement.Properties.Item("rapidFailProtectionMaxCrashes").Value = 5

adminManager.CommitChanges()

Function FindElement(collection, elementTagName, valuesToMatch)
   For i = 0 To CInt(collection.Count) - 1
      Set element = collection.Item(i)
      If element.Name = elementTagName Then
         matches = True
         For iVal = 0 To UBound(valuesToMatch) Step 2
            Set property = element.GetPropertyByName(valuesToMatch(iVal))
            value = property.Value
            If Not IsNull(value) Then
               value = CStr(value)
            End If
            If Not value = CStr(valuesToMatch(iVal + 1)) Then
               matches = False
               Exit For
            End If
         Next
         If matches Then
            Exit For
         End If
      End If
   Next
   If matches Then
      FindElement = i
   Else
      FindElement = -1
   End If
End Function