jump to navigation

Programatically recycle an IIS application pool October 20, 2006

Posted by codinglifestyle in ASP.NET, IIS.
Tags: , , ,

One of the many banes in my life is waiting for IIS to reset.  When developing certain components, like a webpart or webcontrol, I often use a post-build event to gac the assembly and issue an iisreset.  This is necessary for IIS to pick up the latest assembly from the GAC.  Recycling the app pool also forces IIS to reload the assembly, but I never could find a command I could issue from my post-build batch file to automate this.  Well, now I don’t need to look because I just wrote my own utility to recycle the application pool:

using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;

namespace AppPoolRecycler
    class Program
        static void Main(string[] args)
            if (args.Length != 2)
                Console.WriteLine(“Usage:\tapppoolrecycler.exe machine apppool\n\tapppoolrecycler.exe localhost mypool”);
                string sMachine = args[0];
                string sAppPool = args[1];

                string sPath = “IIS://” + sMachine + “/W3SVC/AppPools/” + sAppPool;
                DirectoryEntry w3svc = new DirectoryEntry(sPath);
                w3svc.Invoke(“Recycle”, null);
                Console.WriteLine(“Application pool recycled”);
            catch (Exception ex)
                Console.WriteLine(“Error: ” + ex.Message);

Now in my post build event I can gac my assembly and recycle the app pool which is much faster than a reset.

AppPoolRecycler.exe sanpaula mssharepointportalapppool


Take a look at this blog entry for more information: http://blogs.aspitalia.com/daniele/post555/Riciclare-Application-Pool-IIS-Codice-CSharp.aspx


Update!  I’ve recently come across a way to do this using a script which should be installed on W2003: 

c:\windows\system32\iisapp.vbs /a mssharepointportalapppool /r