.NET C#: Recycle current Application Pool programmatically (for IIS 6+)
I’ve been working on how to recycle the current application pool for my ASP .NET application.
There are 3 steps for doing this:
- Verify if application is running on IIS that supports application pools (if not, there’s nothing to recycle).
- Get application pool name (obtained from the DirectoryServices entry corresponding to our virtual directory).
- Invoke Recycle method in the DirectoryServices entry corresponding to the application pool.
I divided the code into this 3 steps, the RecycleApplicationPool method can be used separately for recycling any application pool (by knowing only its name). The RecycleCurrentApplicationPool method returns a boolean value indicating if the application pool was recycled.
public static class ApplicationPoolRecycle
{
/// <summary>Attempts to recycle current application pool</summary>
/// <returns>Boolean indicating if application pool was successfully recycled</returns>
public static bool RecycleCurrentApplicationPool()
{
try
{
// Application hosted on IIS that supports App Pools, like 6.0 and 7.0
if (IsApplicationRunningOnAppPool())
{
// Get current application pool name
string appPoolId = GetCurrentApplicationPoolId();
// Recycle current application pool
RecycleApplicationPool(appPoolId);
return true;
}
else
return false;
}
catch
{
return false;
}
}
private static bool IsApplicationRunningOnAppPool()
{
// Application is not hosted on IIS
if (!AppDomain.CurrentDomain.FriendlyName.StartsWith("/LM/"))
return false;
// Application hosted on IIS that doesn't support App Pools, like 5.1
else if (!DirectoryEntry.Exists("IIS://Localhost/W3SVC/AppPools"))
return false;
else
return true;
}
private static string GetCurrentApplicationPoolId()
{
string virtualDirPath = AppDomain.CurrentDomain.FriendlyName;
virtualDirPath = virtualDirPath.Substring(4);
int index = virtualDirPath.Length + 1;
index = virtualDirPath.LastIndexOf("-", index - 1, index - 1);
index = virtualDirPath.LastIndexOf("-", index - 1, index - 1);
virtualDirPath = "IIS://localhost/" + virtualDirPath.Remove(index);
DirectoryEntry virtualDirEntry = new DirectoryEntry(virtualDirPath);
return virtualDirEntry.Properties["AppPoolId"].Value.ToString();
}
private static void RecycleApplicationPool(string appPoolId)
{
string appPoolPath = "IIS://localhost/W3SVC/AppPools/" + appPoolId;
DirectoryEntry appPoolEntry = new DirectoryEntry(appPoolPath);
appPoolEntry.Invoke("Recycle");
}
}
Hola Leandro, curso al igual con vos en la UTN y queria saber si tenés conocimientos en MSSQL y PHP orientado a objetos. Ya que en la empresa que estoy trabajando andamos en busqueda de programadores de medio tiempo ( mañana o tarde, dependiendo en el horario que curses) Si te intereza, tenés mi mail.
Saludos,
Pablo.-
Can you be more specific about which libraries should be included for this code?
Of course, the only “rare” one is System.DirectoryServices, available from Add Reference menu => .NET Assemblies.
I’ve updated this to work with WMI instead of DirectoryServices (because of problems with computers where DirectoryServices didn’t work correctly), the only problem is that only IIS 6+ expose to WMI (so XP IIS 5.1 won’t work).
I’ll try to post it ASAP.
What rights/permissions IIS user should have to avoid the Access Denied exception on attempt to Call any-thing on the DirectoryEntry?
Hi, (nos juntamos varios de la UTN parece)…
I have a related question, i have some class library, published with a WCF service application in IIS 6. This class library has some worker threads doing stuff.
How can i receive an event (or something like) when the AppPool of the WCF Service App is recycled to do save, restore, cleanup code…
Thanks che!
What rights/permissions should be set to avoid the UnauthorizedAccessException on appPoolEntry.Invoke(“Recycle”) ?