I have an IIS 6 server and I need to recycle a specific application pool. I need to design an ASP.NET web page using C# that performs this task.
How can I do this?
just make a separated web page / web application and install it on the web server targeting another app pool (not sure how it would work if running as same page of your app and being linked to the same app pool you want to recycle).
then follow instructions here: https://stackoverflow.com/a/496357/559144
You can use the DirectoryEntry class to programmatically recycle an application pool given its name:
var path = "IIS://localhost/W3SVC/AppPools/MyAppPool";
var appPool = new DirectoryEntry(path);
appPool.Invoke("Recycle");
The following should (I can't attest to that as the code hasn't been used in quite some time) suffice:
using System;
using System.Collections.Generic;
using System.Web;
using System.DirectoryServices;
public static class ApplicationPoolRecycle
{
public static void RecycleCurrentApplicationPool()
{
string appPoolId = GetCurrentApplicationPoolId();
RecycleApplicationPool(appPoolId);
}
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");
}
}