應用程式集 < 區回收的回收設定>

概觀

元素 <recycling> 包含組態設定,可控制觸發 IIS 7 重新開機應用程式集區的條件。 您也可以控制應用程式集區回收時,IIS 寫入事件記錄檔的事件種類。

您可以指定 IIS 依設定間隔 (回收應用程式集區,例如每隔 180 分鐘) 、每天的特定時間,或應用程式集區收到特定數目的要求之後。 您也可以將 <recycling> 元素設定為在背景工作進程虛擬記憶體和實體記憶體使用量達到特定閾值時重新開機應用程式集區。

當 IIS 回收應用程式集區時,您可以使用 <recycling> 元素來指定要記錄到事件記錄檔的兩個類別。 第一個類別包含您可以設定的回收事件,例如上一個段落中所述的事件。 第二個類別包括執行時間回收事件,例如隨選回收事件、在應用程式集區中執行之應用程式或應用程式執行之設定變更所觸發的回收事件,或由狀況不良的 Internet Server Application Programming Interface 所觸發的回收事件, (ISAPI) 篩選或 ISAPI 擴充功能。

元素 <recycling> 會使用下列屬性和子項目來實作這些功能:

  • logEventOnRecycle 屬性
  • periodicRestart 元素

相容性

版本 備註
IIS 10.0 屬性的 logEventOnRecycle 預設值已在 IIS 10.0 中修改。
IIS 8.5 未在 IIS 8.5 中修改專案 <recycling>
IIS 8.0 未在 IIS 8.0 中修改專案 <recycling>
IIS 7.5 未在 IIS 7.5 中修改專案 <recycling>
IIS 7.0 元素 <recycling> 是在 IIS 7.0 中引進。
IIS 6.0 元素 <recycling> 會取代 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. 在 [ 動作 ] 窗格中,按一下 [ 回收...
    顯示 I S Manager 的螢幕擷取畫面。回收會列在 [動作] 窗格中。

  5. 在 [編輯應用程式集區回收設定精靈] 的 [回收條件] 頁面上,選取 [ 固定間隔 ] 區段中至少其中一個選項,在適當的文字方塊中輸入值,然後按 [ 下一步]。
    顯示 [編輯應用程式集區回收設定] 對話方塊中 [回收條件] 頁面的螢幕擷取畫面。核取特定時間,並在文字方塊中輸入 3 M。

  6. (選擇性) 在 [編輯應用程式集區回收設定精靈] 的 [回收事件至記錄檔] 頁面上,選取您要 IIS 在事件發生時傳送至事件記錄檔的可設定回收事件和執行時間回收事件,然後按一下 [ 完成]。
    顯示 [回收事件至記錄] 頁面的螢幕擷取畫面。已選取排程的時間。

組態

元素 <recycling> 可在 ApplicationHost.config 檔案的伺服器層級設定。

屬性

屬性 描述
disallowOverlappingRotation 選擇性的 Boolean 屬性。

指定 WWW 服務是否應該啟動另一個背景工作進程,以在該程式關閉時取代現有的背景工作進程。 如果背景工作進程載入任何不支援多個背景工作進程的應用程式程式碼,這個屬性的值應該設定為 true

預設值是 false
disallowRotationOnConfigChange 選擇性的 Boolean 屬性。

指定當設定變更時,WWW 服務是否應該輪替應用程式集區中的背景工作進程。

預設值是 false
logEventOnRecycle 選擇性旗標屬性。

指定 IIS 應該在回收應用程式集區時記錄事件。 如果 IIS 要記錄事件, logEventOnRecycle 屬性必須有對應到回收原因的位。

logEventOnRecycle屬性可以有下列一或多個可能的值。 如果您指定多個值,請使用逗號分隔 (,) 。 IIS 10 之前版本的預設旗標為 、 和 ;若為 IIS 10 和更新版本,則為 TimeRequestsIsapiUnhealthyConfigChangeOnDemandScheduleMemory 和 ; PrivateMemoryPrivateMemoryMemoryTime
描述
ConfigChange 當應用程式集區因為設定變更而回收時,記錄事件。

數值為 64
IsapiUnhealthy 當應用程式集區在 ISAPI 擴充功能回報為處於狀況不良狀態的背景工作進程時,記錄事件。
數值為 16
Memory 當應用程式集區在使用指定數量的虛擬記憶體之後回收時,記錄事件。

數值為 8
OnDemand 當應用程式集區立即回收以修正問題時,請記錄事件。

數值為 32
PrivateMemory 當應用程式集區在使用指定數量的虛擬記憶體之後回收時,記錄事件。

數值為 128
Requests 當應用程式集區在到達設定的要求數目之後回收時,記錄事件。

數值為 2
Schedule 當應用程式集區在達到一天設定的時間之後回收時,記錄事件。

數值為 4
Time 在設定的時間之後,應用程式集區回收時記錄事件。

數值為 1

子元素

元素 描述
periodicRestart 選擇性項目。

指定回收應用程式集區的條件。

組態範例

下列組態範例會使用應用程式集 <add> 區元素來建立名為 Contoso 的新應用程式集區。 元素 <recycling> 會設定應用程式集區重新開機的記錄、 <periodicRestart> 專案會在應用程式集區重新開機時設定,而 <processModel> 元素會設定 shutdownTimeLimitstartupTimeLimit 屬性,以關閉應用程式集區中的背景工作進程,每 30 秒啟動背景工作進程。 如果超過這些時間限制,IIS 會終止背景工作進程。

<add name="Contoso">
   <recycling logEventOnRecycle="Schedule">
      <periodicRestart>
         <schedule>
            <clear />
            <add value="03:00:00" />
         </schedule>
      </periodicRestart>
   </recycling>
   <processModel identityType="NetworkService" shutdownTimeLimit="00:00:30" startupTimeLimit="00:00:30" />
</add>

範例程式碼

下列程式碼範例會將名為 Contoso 的應用程式集區新增至 IIS 7 伺服器,然後將應用程式集區設定為每天上午 3:00 回收。

AppCmd.exe

appcmd.exe set config -section:system.applicationHost/applicationPools /+"[name='Contoso']" /commit:apphost

appcmd.exe set config -section:system.applicationHost/applicationPools /+"[name='Contoso'].recycling.periodicRestart.schedule.[value='03:00:00']" /commit:apphost

您也可以使用下列語法:

appcmd.exe add apppool /name:"Contoso"

appcmd.exe set config -section:system.applicationHost/applicationPools /+"[name='Contoso'].recycling.periodicRestart.schedule.[value='03:00:00']" /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 = applicationPoolsCollection.CreateElement("add");
         addElement["name"] = @"Contoso";
         ConfigurationElement recyclingElement = addElement.GetChildElement("recycling");
         ConfigurationElement periodicRestartElement = recyclingElement.GetChildElement("periodicRestart");
         ConfigurationElementCollection scheduleCollection = periodicRestartElement.GetCollection("schedule");
         ConfigurationElement addElement1 = scheduleCollection.CreateElement("add");
         addElement1["value"] = TimeSpan.Parse("03:00:00");
         scheduleCollection.Add(addElement1);
         applicationPoolsCollection.Add(addElement);
         serverManager.CommitChanges();
      }
   }
}

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 = applicationPoolsCollection.CreateElement("add")
      addElement("name") = "Contoso"
      Dim recyclingElement As ConfigurationElement = addElement.GetChildElement("recycling")
      Dim periodicRestartElement As ConfigurationElement = recyclingElement.GetChildElement("periodicRestart")
      Dim scheduleCollection As ConfigurationElementCollection = periodicRestartElement.GetCollection("schedule")
      Dim addElement1 As ConfigurationElement = scheduleCollection.CreateElement("add")
      addElement1("value") = TimeSpan.Parse("03:00:00")
      scheduleCollection.Add(addElement1)
      applicationPoolsCollection.Add(addElement)
      serverManager.CommitChanges()
   End Sub
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 addElement = applicationPoolsCollection.CreateNewElement("add");
addElement.Properties.Item("name").Value = "Contoso";
var recyclingElement = addElement.ChildElements.Item("recycling");
var periodicRestartElement = recyclingElement.ChildElements.Item("periodicRestart");
var scheduleCollection = periodicRestartElement.ChildElements.Item("schedule").Collection;
var addElement1 = scheduleCollection.CreateNewElement("add");
addElement1.Properties.Item("value").Value = "03:00:00";
scheduleCollection.AddElement(addElement1);
applicationPoolsCollection.AddElement(addElement);

adminManager.CommitChanges();

VBScript

Set adminManager = createObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set applicationPoolsSection = adminManager.GetAdminSection("system.applicationHost/applicationPools", "MACHINE/WEBROOT/APPHOST")
Set applicationPoolsCollection = applicationPoolsSection.Collection

Set addElement = applicationPoolsCollection.CreateNewElement("add")
addElement.Properties.Item("name").Value = "Contoso"
Set recyclingElement = addElement.ChildElements.Item("recycling")
Set periodicRestartElement = recyclingElement.ChildElements.Item("periodicRestart")
Set scheduleCollection = periodicRestartElement.ChildElements.Item("schedule").Collection
Set addElement1 = scheduleCollection.CreateNewElement("add")
addElement1.Properties.Item("value").Value = "03:00:00"
scheduleCollection.AddElement(addElement1)
applicationPoolsCollection.AddElement(addElement)

adminManager.CommitChanges()