[译]IIS 8.0应用初始化

时间:2022-05-28 06:36:18

原文

代码 或者点

通过IIS 8.0应用初始化特性管理员可以配置IIS为一个网站或多个网站提前执行初始化任务。当应用在初始化期间,可以通过配置先返回一个静态页面知道应用的初始化任务完成。

通过配置一系列的全局级和应用级规则可以控制如何/何时初始化网站应用。

指南

事前准备

首先需要安装IIS 8.0。另外,应用初始化特性是作为IIS的"Application Development"子特性提供的,也需要安装。

下面的截图来自于Windows Server 2012 Server Manager UI,展示了如何安装Application Initialization特性

[译]IIS 8.0应用初始化

全局应用初始化

可以在两个地方配置应用初始化特性:全局级别的applicationHost.config文件,和应用级别的web.config文件。

In this walkthrough, you will configure a sample application to always be initialized when the application pool associated with the application starts up. Since application pool behaviors can only be configured in applicationHost.config, running application initialization whenever an application pool starts up is considered part of the "global" application initialization settings.

修改applicationHost.config

用记事本打开%WINDIR%\system32\inetsrv\config文件夹中的applicationHost.config文件。

找到配置块,找到名为".NET v4.5"的应用池记录。

修改这个应用池记录让这个应用池是always running的状态。

<add name=".NET v4.5" startMode="AlwaysRunning" managedRuntimeVersion="v4.0" />

往下找到配置元素。在配置元素中有一个

<application path="/appinit" preloadEnabled="true" applicationPool=".NET v4.5">

设置preloadEnable为true tells IIS 8.0 that it sends a "fake" request to the application when the associated application pool starts up. That is why in the previous step we set the application pool's startMode to "AlwaysRunning".

With the combination of the application pool always running, and the application itself being marked to always receive a fake request, whenever the machine restarts and/or the World Wide Web service is recycled, IIS 8.0 ensures that the application pool instance is running and that the application "/appinit" is always sent a fake request to trigger the application to start up.

修改web.config

用记事本打开位于网站所在目录C:\inetpub\wwwroot\appinit中的web.config。

web.config已经设置好了一些section,但是被注释了,先取消<system.webServer>中的注释。

<applicationInitialization
remapManagedRequestsTo="Startup.htm"
skipManagedModules="true" >
<add initializationPage="/default.aspx" />
</applicationInitialization>

这个配置告诉IIS在初始化完成前,返回Startup.html页面给所有请求者。

运行应用

net stop w3svc & net start w3svc

用浏览器打开http://localhost/appinit/default.aspx

浏览器先是显示“Startup.htm”这个页面,一旦应用初始化完成,便会返回真正的请求页面。

配置overlapped进程回收

IIS 8.0通过在一个后台overlapped进程中执行应用初始化集成了应用初始化和overplapped进程回收。当IIS检查到一个活动的工作进程在被回收的时候,不会马上转到新的工作进程中,而是等新的进程完成了初始化工作后才转到这个新的进程。这样保证了当应用已经在运行的时候不会再次看到“Startup.html”页面。

打开applicationHost.config文件。修改如下:

<add name=".NET v4.5"
startMode="AlwaysRunning"
managedRuntimeVersion="v4.0" >
<recycling logEventOnRecycle="Schedule">
<periodicRestart requests="30" />
</recycling>
</add>

元素告诉IIS每30个HTTP请求回收进程。

运行应用

net stop w3svc & net start w3svc

用浏览器打开http://localhost/appinit/default.aspx

“Startup.htm”展现出来了

打开任务管理器。按照进程名排序,可以看到有一个w3wp.exe线程,状态为Running。这个就是在运行"appinit"应用的进程。

[译]IIS 8.0应用初始化

不断刷新浏览器直到出现了真正的default.aspx页面。

现在再次刷新页面30次以上,导致IIS回收应用池。现在停止刷新,回到任务管理器,可以看到出现了第二个w3wp.exe进程:

[译]IIS 8.0应用初始化

上面的截图告诉我们当进程开始回收的时候第二个w3wp.exe开始了。

再次刷新浏览器,我们看到的依然是default.aspx页面。即使应用初始化正在这个新的w3wp.exe实例中进行。

URL Rewrite与应用初始化