How to Programmatically Recycle Application Pools In C#
The requirement recently came up for me that I had to recycle application pools programmatically from a custom Windows Service that I was using, which I have come to use to recycle application pools when deploying new WebParts to the GAC through code techniques. I thought that this piece of code might be helpful for others that had to achieve the same task. The method that is being developed is called RecycleIISWorkProcess(), and will recycle all application pools on the SharePoint instance that the piece of code is run.
The first thing that you have to do is to make the appropriate references in your new class file. The important one to notice is the System.Management namespace reference. If you like writing code to manage and work with system objects on your machine (we all know I do). , then the System.Management namespace will appeal to you. For example, the System.Management namespace will allow you to do things like get the manufacturer using code supplemented through the ComputerSystem class using the Manufacturer property and parsing it ToString() in a user interface element.
- using System;
- using System.Management;
The class that is being developing is marked with the internal access modifier so that it is only available to types and members in the same assembly, and doesn’t take any parameters.
- internal static void RecycleIISWorkerProcess()
The main workhorse class is the ManagementObjectSearcher class out of the System.Management namespace. The ManagementObjectSearcher class is an important programmatic management object since it provides the entry point to most management actions against the server objects, such as disks and other pieces of hardware. As you can see later in this method, allowing this class can take a WMI query.
We can see that we are returning a collection of the available application pools (essentially server objects fit for management) for the SharePoint server instance through the query SELECT * FROM IIsApplicationPool.
- ManagementObjectSearcher searcher =
- new ManagementObjectSearcher
- (@"root\microsoftiisv2", "SELECT * FROM IIsApplicationPool");
Now that the collection has been retrieved, you have to iterate through the collection and recycle the appropriate assets. Use a foreach() loop to go through each WMI instance and using the Get() method out of ManagementObjectSearcher in order to run the WMI query asynchronously, which will bind it to the watcher to deliver the final results of the execution. After, the recycle method will be called asynchronously using the InvokeMethod by passing in the recycle string, and since there are no options that are executed with the method, a null value passed in as the other parameter.
- foreach (ManagementObject obj in searcher.Get())
- obj.InvokeMethod("Recycle", null);
Now you can use the method to execute recycling of application pools through code! For example, you could check whether the assembly is currently present within the GAC using System.GAC.AssemblyCache and testing whether the assembly currently exists through ContainsAssemblyName which will return a Boolean value of whether the passed assembly name parameter is present in the GAC.
- if (AssemblyCache.ContainsAssemblyName(assemblyName))
Then you can simply call your new IIS recycle method when the if statement is satisfied.