Recycling IIS application pools and COM+ programmatically in C#

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.

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.");
            }
        }
    }
}

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

Cheers

Recycling IIS application pools and COM+ programmatically in C#

5 thoughts on “Recycling IIS application pools and COM+ programmatically in C#

  1. VMware Certified Advanced Professional 6 (Desktop and Mobility Deployment) – The industry-recognized VCAP6-DTM Deploy certification validates that you know how to deploy and optimize VMware Horizon 6 (with View) environments. It proves that you have the knowledge and skills vital to leverage best practices to provide a scalable and reliable Business Mobility platform for your organization. Some of the subject areas involve: Configuring and managing Horizon View components, configuring cloud pod archituecture, configuring Group Policy settings related to Horizon View, Configuring and optimizing desktop images for Horizon View & Mirage, Configuring and managing App Volumes AppStacks, Configuring desktop pools, Configuring and deploying ThinApp packaged applications, Configuring VMWare Identity Manager, etc.Sebastian’s take on the VCAP6 exam: “In my thoughts and opinions VCAP6 exam is way better experience in comparison with VCAP5, the newest exam appears exactly like VMware HOL. The interface is a breeze, questions are structured on the right side of the display, and can be concealed to the side or even restored when necessary. My bits of advice to the questions windowpane: if you want to make it floating, you better know how to restore it back. I ended up shifting it all around simply because I fail to remember how to recover it back. The two arrows that appeared to be buttons on the top were designed to dock the window to right of left. Fonts can be resized, which i think was far better than scrolling up and down the question. The response speed of the whole user interface was much quicker in comparison with VCAP5.5, and there wasn’t any lagging time period experienced when changing from window to window. Something to bear in mind: BACKSPACE key is not working! I believe it is good as you don’t reload your exam window in error, nevertheless, it can be irritating sometimes whenever you type some thing incorrectly and you need to select and press Del to remove. The Desktop and shortcuts were arranged adequately, and all needed applications like web browser or Mirage console can be easily launched. There is a excellent interface for Remote Desktop Manager and you’ll discover all needed RDP connection to servers or desktops without having to type username and password. The web browser had all the links in the Favorite Bar. At the time I am penning this, there’s no extra Thirty minute extension for Non-Native English speaker at No-Native English country, which is actually a bummer. There are actually 39 question to respond within the three hours time, and this can be actually really hard for non-native English speakers like me. A number of questions take time to complete, so it’s preferable to drop the questions that you can’t answer, and finish those you can. After the 39 questions, you’ll be able to get back to the uncompleted questions if you still have time. Never waste too much time on one single question! The examination blue print can be obtained on my web site at Szumigalski.com. It is well organized and following it for the exam preparation should help a lot. Of course, the most effective is if you could have numerous practical experience! I’m actually extremely pleased with the examination experience, although I passed this time by small margin, however i know very well what I missed for the exam, learn from the blunders and practice harder to acquaint myself with the environment. This certification is going to open up your job prospects!”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s