定期重新開機應用程式集區回收 < 定期重新開機設定>

概觀

元素 <periodicRestart> 包含組態設定,可讓您控制何時回收應用程式集區。 您可以指定 Internet Information Services (IIS) 7 在時間間隔 (幾分鐘) 或每天的特定時間回收應用程式集區。 您也可以將 IIS 設定為根據應用程式集區中背景工作進程正在使用的虛擬記憶體或實體記憶體數量,或在背景工作進程處理特定數目的要求之後設定 IIS 來回收應用程式集區。

相容性

版本 備註
IIS 10.0 未在 IIS 10.0 中修改專案 <periodicRestart>
IIS 8.5 未在 IIS 8.5 中修改專案 <periodicRestart>
IIS 8.0 未在 IIS 8.0 中修改專案 <periodicRestart>
IIS 7.5 未在 IIS 7.5 中修改專案 <periodicRestart>
IIS 7.0 元素 <periodicRestart> 是在 IIS 7.0 中引進。
IIS 6.0 元素 <periodicRestart> 會取代 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. 在 [ 動作 ] 窗格中,按一下 [ 回收...
    [動作] 窗格的螢幕擷取畫面。在 [編輯應用程式集區] 下,反白顯示 [回收]。

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

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

組態

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

屬性

屬性 描述
memory 選擇性 uint 屬性。

指定在回收背景工作進程之前,背景工作進程可以使用的虛擬記憶體 (,以 KB 為單位) 。

預設值為 0 ,這會停用 屬性。
privateMemory 選擇性 uint 屬性。

指定背景工作進程可以在背景工作進程回收之前使用) 以 KB 為單位的私人記憶體 (數量。

預設值為 0 ,這會停用 屬性。
requests 選擇性 uint 屬性。

指定在處理特定數目的要求之後,應該回收背景工作進程。

預設值為 0 ,這會停用 屬性。
time 選擇性的 timeSpan 屬性。

指定在經過指定的時間量之後,應該回收背景工作進程。

預設值為 29:00:00 (29 小時) 。

子元素

元素 描述
schedule 選擇性項目。

指定應用程式集區定期重新開機的排程。

組態範例

下列組態範例會使用應用程式集 <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()