Click here to Skip to main content
6,629,377 members and growing! (20,431 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Howto     Intermediate License: The Code Project Open License (CPOL)

Recycling IIS 6.0 application pools programmatically

By Alberto Venditti

A simple way to recycle IIS 6.0 application pools programmatically.
VB, Windows (Win2003), .NET (.NET 2.0), ASP.NET, WebForms, Dev
Posted:27 May 2008
Views:21,401
Bookmarked:31 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
7 votes for this article.
Popularity: 3.86 Rating: 4.57 out of 5

1

2

3
2 votes, 28.6%
4
5 votes, 71.4%
5

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 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:

<%@ 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)

About the Author

Alberto Venditti


Member
I was born in 1970.

I studied Electronic Engineering (graduated in 1997).

Subsequently, I passed some Microsoft exams, and currently I'm certified as:
MCP, MCT, MCDBA, MCSD, MCAD, MCSD for .NET (early achiever).

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.
Occupation: Web Developer
Location: Italy Italy

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
GeneralProblems with code Pinmemberchmodlf14:00 2 Sep '09  
GeneralRe: Problems with code PinmemberAlberto Venditti22:53 2 Sep '09  
Generaliis 7.0 Pinmemberesrah0:55 27 Feb '09  
GeneralRe: iis 7.0 PinmemberAlberto Venditti1:39 27 Feb '09  
GeneralRe: iis 7.0 Pinmemberesrah1:41 27 Feb '09  
GeneralRe: iis 7.0 PinmemberAlberto Venditti3:09 27 Feb '09  
GeneralCommand line option Pinmembernewkie1:33 3 Jun '08  
GeneralMeasures PinmemberChiew Heng Wah17:29 2 Jun '08  
GeneralRe: Measures PinmemberAlberto Venditti22:49 2 Jun '08  
GeneralSorry to ask ... PinmemberESTAN5:28 28 May '08  
GeneralRe: Sorry to ask ... Pinmemberastanton19789:39 28 May '08  
GeneralRe: Sorry to ask ... PinmemberAlberto Venditti13:48 28 May '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 27 May 2008
Editor: Smitha Vijayan
Copyright 2008 by Alberto Venditti
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project