How can I programmatically recycle a .net web app's own apppool?
The following code will recycle the current site's app pool. You need to add a reference to Microsoft.Web.Administration
using (ServerManager iisManager = new ServerManager())
{
SiteCollection sites = iisManager.Sites;
foreach (Site site in sites)
{
if (site.Name == HostingEnvironment.SiteName)
{
iisManager.ApplicationPools[site.Applications["/"].ApplicationPoolName].Recycle();
break;
}
}
}
The simplest way to "trip" the ASP.NET worker process into recycling an application pool is to modify the web.config file in some way. This change is picked up by the file system watcher and causes ASP.NET to recycle in order to load the new configuration.
The content of the file doesn't have to change in any practical way; just adding or removing whitespace character is enough.
Edit:
If this isn't strong enough to work around your problem, you can go the whole hog and use Directory Services to recycle the app pool manually.
// Set up the path identifying your application pool.
var path = "IIS://YOURSERVERNAME/W3SVC/AppPools/YourAppPoolName";
// Create the directory entry to control the app pool
var appPool = new DirectoryEntry(path);
// Invoke the recycle action.
appPool.Invoke("Recycle", null);
Based on Code Project: Recycling IIS 6.0 application pools programmatically.