<2017 September>
SunMonTueWedThuFriSat
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

On this page...

Search

Links

Member of...


ASP Insiders

MVP Visual Developer ASP/ASP.NET

Enter CodeZone

Blog Categories

Microsoft

Blogroll

Deutsche Resourcen

Management

Sign In
 

#  Tuesday, 23 May 2006

Yesterday after my talk at MS' Big>Days 2006 in Vienna I was asked how to recycle an IIS app pool from within an application / script / code. I knew I had seen it somewhere before, so I promised to post the information in my blog as soon as I had dug it up.

There are actually a few others that have posted that information before, for example on the aspitalia.com blogs - Riciclare un application pool di IIS 6 da codice C#. It does exactly what the post title implies: recycling an application pool with C#. This approach uses ADSI (aka System.DirectoryServices) to do the bidding, and I have the non-ASP.NET bound version here:

using System.DirectoryServices;

...

public void RecycleAppPool(string machine, string appPoolName)
{
string path = "IIS://" + machine + "/W3SVC/AppPools/" + appPoolName;

DirectoryEntry w3svc = new DirectoryEntry(path);
w3svc.Invoke("Recycle", null);
}

So, now the question arises - how do I know the names of the app pools? One way is to enumerate all the existing application pools on a box - the blog post Control the Application Pool shows how to pull it off using WMI.

Finally, I went to the authoritative source, Chris Adams blog. He has a post up titled Recycling Application Pools using WMI in IIS 6.0, so this is along the lines of the previous one. He has samples in VBScript as well as C# online. He also shows a quick way (end of the post) how to find out which app pool is servicing which IIS Web application.

I think this should cover the topic nicely ;-) Also looking forward to how easy recycling app pools will be in IIS7.

Categories: IIS
Tuesday, 23 May 2006 08:20:30 (W. Europe Daylight Time, UTC+02:00)  #    Comments [1]

 



Thursday, 02 November 2006 19:53:39 (W. Europe Standard Time, UTC+01:00)
I couldn't bring up the link you have to Chris Adams blog...however, you can do it using the Directory Service built into .NET 2.0...I did it for a console app, where I allowed the user to type in a full/partial name of an application pool to recycle...(or they could specify no parameters, and they would get a full list of the application pools)

Here's the code:


static void Main(string[] args)
{
DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");

if (appPools == null)
{
Console.WriteLine("There were no App Pools located");
return;
}

if (args.Length == 0)
{
Console.WriteLine("Please specify an App Pool to recycle: ");

foreach (DirectoryEntry row in appPools.Children)
{
Console.WriteLine(row.Name.ToString());
}

return;
}

int restartCount = 0;

foreach (DirectoryEntry row in appPools.Children)
{
if (row.Name.ToLower().IndexOf(args[0]) > -1)
{
try
{
row.Invoke("Recycle", null);
restartCount += 1;
Console.WriteLine("Recycling App Pool: " + row.Name);
}
catch (Exception ex)
{
Console.WriteLine("There was an error recycling the App Pool: " + row.Name);
Console.WriteLine("Details: " + ex.Message);
}
}
}

if (restartCount == 0)
{
Console.WriteLine("Could not locate the App Pool specified.");
foreach (DirectoryEntry row in appPools.Children)
{
Console.WriteLine(row.Name.ToString());
}
}

}
Jason Rastovski
Comments are closed.

© Copyright 2017 Christoph Wille

newtelligence dasBlog 2.3.9074.18820
Subscribe to this weblog's RSS feed with SharpReader, Radio Userland, NewsGator or any other aggregator listening on port 5335 by clicking this button.   RSS 2.0|Atom 1.0  Send mail to the author(s)

 
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.