Click here to Skip to main content
15,867,308 members
Articles / Web Development / ASP.NET
Article

Recycling IIS 6.0 application pools programmatically

Rate me:
Please Sign up or sign in to vote.
4.57/5 (7 votes)
27 May 2008CPOL2 min read 107.9K   34   18
A simple way to recycle IIS 6.0 application pools programmatically.

Introduction

The need for programmatically executing an application pool recycle can arise for a number of reasons. It happened to me while I was managing lots of similar ASP.NET web applications, hosted on multiple IIS 6.0 web servers, and I had to find a way to reset their cache in a quick and easy way. In particular, I was using the WebSettings mechanism for storing my ASP.NET application configuration settings and, due to the number of web apps to be contacted on many web servers, the multiple calls to GetWebSetting("", True) were becoming unmanageable. I found a "quick and easy way" to recycle application pools (hence, also, to reset the ASP.NET cache) through the DirectoryEntry .NET Framework class, and now I'm sharing this with you.

Recycling the web from the web

I was looking for a way to recycle application pools without the need to connect to the web server (via a Terminal Server connection, or connecting remotely from my local IIS MMC console); as stated, this was useful, especially considering the lots of application pools distributed on more than ten web servers... My first thought was to develop a Windows Forms application (one of my famous "tools"), capable of interacting in some way with IIS or with its metabase, in order to execute an AppPool recycle with a single click. But, then I realized it was more natural to create this piece of code directly inside an ASP.NET application. So, I wrote a single, very "light", ASP.NET page.

This web page needs an XML data file (let's name it ApplicationPoolsList.xml) where the names and paths of all the desired application pools are listed; in fact, my goal was to recycle, at once, a set of "similar" AppPools. This is a sample of the XML data file:

XML
<?xml version="1.0" encoding="utf-8" ?>
<AppPoolsList>
  <AppPool>IIS://SERVER00123/W3SVC/AppPools/Northwind</AppPool>
  <AppPool>IIS://SERVER00123/W3SVC/AppPools/Acme</AppPool>
  <AppPool>IIS://SERVER00125/W3SVC/AppPools/Northwind</AppPool>
  <AppPool>IIS://SERVER00125/W3SVC/AppPools/Acme</AppPool>
</AppPoolsList>

Each "AppPool" node contains the full path of an application pool to be recycled, including the web server machine name and the AppPool name as it appears on the IIS management console.

For example, in "IIS://SERVER00123/W3SVC/AppPools/Northwind":

  • "SERVER00123" is the web server machine name (as returned by System.Environment.MachineName when run on that machine);
  • "Northwind" is the AppPool name as it appears on the IIS management console when looking at AppPools hosted on SERVER00123.

The ASP.NET page that does the magic is very simple, and it is coded as follows:

VB
<%@ Page Language="vb" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Management" %>
<%@ Import Namespace="System.DirectoryServices" %>
<%
  ' Utility page for AppPool recycling (by Alberto Venditti, 20/05/2008)
  Response.Write("<HTML><HEAD></HEAD><BODY>")

  Dim AppPoolsList As New XmlDocument
  AppPoolsList.Load(Server.MapPath("ApplicationPoolsList.xml"))
  Dim AppPool As XmlNode
  For Each AppPool In AppPoolsList.SelectNodes("AppPoolsList/AppPool")
    Dim AppPoolFullPath As String = AppPool.InnerText
    ' AppPoolFullPath must be in the form of: "IIS://" + machine + _
    '       "/W3SVC/AppPools/" + appPoolName
    Try
      Dim w3svc As New DirectoryEntry(AppPoolFullPath)
      w3svc.Invoke("Recycle", Nothing)
      Response.Write(AppPoolFullPath & "<br />")
    Catch
      Response.Write(AppPoolFullPath & " [error]<br />")
    End Try
  Next
 
  Response.Write("<p />-- done --")
  Response.Write("</BODY></HTML>")
%>

That's all.

The heart of the execution consists of a call to the Invoke method on the DirectoryEntry class (be aware that this call could suffer restrictions depending upon the permission level of the ASP.NET account). I know: it's not a great and wonderful piece of code, but I found it very, very useful to reset the ASP.NET Cache of multiple applications on multiple servers.

Interesting note: you can also use this page to recycle the AppPool that hosts the web application that is serving the page itself. The AppPool is recycled immediately, after the execution of the page terminates.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
Italy Italy
I was born in 1970.

My first computer experience dates back to early 80s, with a Sinclair ZX81.
From that time on, as many "friends" say, my IT-illness has increased year by year.

I graduated in Electronic Engineering and earned the following Microsoft certifications:
MCP, MCT, MCDBA, MCSD, MCAD, MCSD for .NET (early achiever).

I worked in IT as a developer, a teacher, a consultant, a technical writer, a technical leader.
IT knowledge applied to real life is my primary interest and focus.

Comments and Discussions

 
QuestionAccess is denied. Pin
ccheeseman30-Jun-11 9:09
ccheeseman30-Jun-11 9:09 
AnswerRe: Access is denied. Pin
Alberto Venditti3-Jul-11 22:29
Alberto Venditti3-Jul-11 22:29 
GeneralThe RPC server is unavailable. Pin
TsetsE12-Apr-11 3:07
TsetsE12-Apr-11 3:07 
GeneralRe: The RPC server is unavailable. Pin
TsetsE12-Apr-11 3:08
TsetsE12-Apr-11 3:08 
GeneralRe: The RPC server is unavailable. Pin
Alberto Venditti14-Apr-11 0:09
Alberto Venditti14-Apr-11 0:09 
GeneralProblems with code Pin
chmodlf2-Sep-09 13:00
chmodlf2-Sep-09 13:00 
GeneralRe: Problems with code Pin
Alberto Venditti2-Sep-09 21:53
Alberto Venditti2-Sep-09 21:53 
Generaliis 7.0 Pin
esrah26-Feb-09 23:55
esrah26-Feb-09 23:55 
GeneralRe: iis 7.0 Pin
Alberto Venditti27-Feb-09 0:39
Alberto Venditti27-Feb-09 0:39 
GeneralRe: iis 7.0 Pin
esrah27-Feb-09 0:41
esrah27-Feb-09 0:41 
GeneralRe: iis 7.0 Pin
Alberto Venditti27-Feb-09 2:09
Alberto Venditti27-Feb-09 2:09 
GeneralCommand line option Pin
Chris_Green3-Jun-08 0:33
Chris_Green3-Jun-08 0:33 
GeneralMeasures Pin
Chiew Heng Wah2-Jun-08 16:29
Chiew Heng Wah2-Jun-08 16:29 
GeneralRe: Measures Pin
Alberto Venditti2-Jun-08 21:49
Alberto Venditti2-Jun-08 21:49 
GeneralSorry to ask ... Pin
ESTAN28-May-08 4:28
ESTAN28-May-08 4:28 
GeneralRe: Sorry to ask ... Pin
astanton197828-May-08 8:39
astanton197828-May-08 8:39 
GeneralRe: Sorry to ask ... Pin
Alberto Venditti28-May-08 12:48
Alberto Venditti28-May-08 12:48 
GeneralRe: Sorry to ask ... Pin
catellostefano25-Dec-09 23:05
catellostefano25-Dec-09 23:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.