These days, I work on projects that is based on Sitecore CMS. Sitecore caches everything, so if you make any changes to your code, the web page does not reflect your change until you reset IIS or recycle the application pool. Resetting IIS often takes 10 to 20, even 30 seconds, and it is a bit obstructive that you have to reset IIS in the middle of coding. So I wrote this small utility.

The way it works is straightforward. It accesses IIS application pools and recycle them all then access COM+ and recycle the specific application that my web application uses for database access. One tricky thing is to access COM+. You have to reference ComAdmin.dll in C:\windows\system32\Com and use COM interop. I could not find many examples on the Internet but finally found one at http://www.dotnet247.com/247reference/msgs/13/67515.aspx.

These are the references I found through google search.

This is my code.

[sourcecode language="csharp"]
class Program
{
static void Main(string[] args)
{
ResetIIS();
RecycleComPlus();
}

private static void ResetIIS()
{
DirectoryEntry appPools = new DirectoryEntry("IIS://localhost/W3SVC/AppPools");
Console.WriteLine("Recycling the following application pools...\n");

foreach (DirectoryEntry pool in appPools.Children)
{
try // if some app pools are turned off, it breaks.
{
pool.Invoke("Recycle", null);
Console.WriteLine(pool.Name);
}
catch (Exception ex) {}
}

Console.WriteLine("\nCompleted...\n");
}

private static void RecycleComPlus()
{
COMAdminCatalog catalog = new COMAdminCatalog();
COMAdminCatalogCollection applications = (COMAdminCatalogCollection)catalog.GetCollection("Applications");
applications.Populate();

foreach (COMAdminCatalogObject app in applications)
{
if (string.Compare(app.Name.ToString(), "DataAccess") == 0)
{
Console.Write("Shutting down " + app.Name + "..");
catalog.ShutdownApplication(app.Name.ToString());
Console.WriteLine(". completed.");
}
}
}
}
[/sourcecode]

Hope this helps and leave comment if you have any idea to improve it.

Cheers


Andrew Chaa

another day, another PR