概述
<recycling> 元素包含配置设置,可用于控制触发 IIS 7 重启应用程序池的条件。 你还可以控制应用程序池回收时 IIS 写入事件日志的事件类型。
可以指定 IIS 按设定的时间间隔(例如每 180 分钟)、在每天的特定时间或在应用程序池收到一定数量的请求后回收应用程序池。 还可以配置 <recycling> 元素,以便在工作进程虚拟内存和物理内存使用量达到特定阈值时重启应用程序池。
可以使用 <recycling> 元素指定当 IIS 回收应用程序池时要记录到事件日志中的两类事件。 第一类包括可配置的回收事件,例如上一段中提到的事件。 第二类包括运行时回收事件,例如按需回收事件、由应用程序池中运行的应用程序中的配置更改触发的回收事件、或由运行不正常的 Internet 服务器应用程序编程接口 (ISAPI) 筛选器或 ISAPI 扩展引起的回收事件。
<recycling> 元素使用以下属性和子元素来实现这些功能:
- logEventOnRecycle 属性
- periodicRestart 元素
兼容性
| 版本 | 说明 | 
|---|---|
| IIS 10.0 | IIS 10.0 中修改了 logEventOnRecycle属性的默认值。 | 
| IIS 8.5 | <recycling>元素在 IIS 8.5 中未进行修改。 | 
| IIS 8.0 | <recycling>元素在 IIS 8.0 中未进行修改。 | 
| IIS 7.5 | <recycling>元素在 IIS 7.5 中未进行修改。 | 
| IIS 7.0 | <recycling>元素是在 IIS 7.0 中引入的。 | 
| IIS 6.0 | <recycling>元素取代了 IIS 6.0 IIsApplicationPools 元数据库属性的部分内容。 | 
安装
<applicationPools> 集合包含在 IIS 7 的默认安装中。
操作方式
如何为应用程序池设置定期回收
- 打开 Internet Information Services (IIS) 管理器: - 如果使用的是 Windows Server 2012 或 Windows Server 2012 R2: - 在任务栏上,单击“服务器管理器”,单击“工具”,然后单击“Internet Information Services (IIS)管理器”。
 
- 如果使用的是 Windows 8 或 Windows 8.1: - 按住 Windows 键,按字母 X,然后单击“控制面板”。
- 单击“管理工具”,然后双击“Internet Information Services (IIS)管理器”。
 
- 如果使用的是 Windows Server 2008 或 Windows Server 2008 R2: - 在任务栏上,单击“开始”,指向“管理工具”,然后单击“Internet Information Services (IIS)管理器”。
 
- 如果使用的是 Windows Vista 或 Windows 7: - 在任务栏上,单击“开始”,然后单击“控制面板”。
- 双击“管理工具”,然后双击“Internet Information Services (IIS)管理器”。
 
 
- 在“连接”窗格中,展开服务器名称,然后单击“应用程序池”。 
- 在“应用程序池”窗格中,选择要编辑的应用程序池。 
- 在“编辑应用程序池回收设置向导”的“回收条件”页上,选择“固定间隔”部分中的至少一个选项,在相应的文本框中输入值,然后单击“下一步”。 
  
- (可选)在“编辑应用程序池回收设置向导”的“回收要记录的事件”页上,选择希望 IIS 在其发生时发送到事件日志的可配置回收事件和运行时回收事件,然后单击“完成”。 
  
配置
<recycling> 元素可在 ApplicationHost.config 文件中的服务器级别进行配置。
特性
| 属性 | 说明 | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| disallowOverlappingRotation | 可选布尔属性。 指定 WWW 服务是否应在现有工作进程关闭时启动另一个工作进程来替代该进程。 如果工作进程加载任何不支持多工作进程的应用程序代码,则应将此属性的值设置为 true。 默认值为 false。 | ||||||||||||||||||
| disallowRotationOnConfigChange | 可选布尔属性。 指定当配置更改时,WWW 服务是否应轮换应用程序池中的工作进程。 默认值为 false。 | ||||||||||||||||||
| logEventOnRecycle | 可选 flags 属性。 指定 IIS 应在回收应用程序池时记录事件。 如果 IIS 要记录事件,logEventOnRecycle 属性必须具有与回收原因对应的位集。 logEventOnRecycle 属性可为以下一个或多个值。 如果指定多个值,请用逗号 (,) 分隔。 早于 IIS 10 的 IIS 版本的默认标志为 Time、Memory和PrivateMemory;对于 IIS 10 及更高版本,为Time、Requests、Schedule、Memory、IsapiUnhealthy、OnDemand、ConfigChange和PrivateMemory;
 | 
子元素
| 元素 | 说明 | 
|---|---|
| periodicRestart | 可选元素。 指定回收应用程序池的条件。 | 
配置示例
以下配置示例使用应用程序池 <add> 元素创建名为 Contoso 的新应用程序池。 <recycling> 元素配置应用程序池重启的日志记录,<periodicRestart> 元素配置应用程序池重启的时间,<processModel> 配置 shutdownTimeLimit 和 startupTimeLimit 属性,用于关闭和启动应用程序池中的工作进程,时间各为 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 配置这些设置时,必须确保将 commit 参数设置为 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()
