If you're working with ASP.NET Web Application published on IIS Web Servers you most likely already know what an Application Pool (aka App Pool) is: in a nuthsell, Application Pools allow you to isolate your web applications from one another, even if they are running on the same server. This way, if there is an error in one app, it won't take down other applications. Additionally, applications pools allow you to separate different apps which require different levels of security.
For further info about App Pools I strongly suggest to take a look at this TechNet article: for a more practical overview and some valuable configuration code samples I also suggest this great post on the developer.com website, which basically explains everything you need to know about them.
By reading the above resources you will find that Application Pools can be periodically recycled to avoid unstable states that can lead to application crashes, hangs, or memory leaks. The App Pool's recycling behaviour can be configured using the IIS Manager GUI and can be expressed in minutes, specific time of day and so on. You can also manually recycle an Application Pool from the GUI by using the Recycle... option, available by right-cliking on the application pool itself and also in the right column of the IIS Manager interface.
What if we want to programmatically recycle an Application Pool from a C# Application? Doing that is rather easy, even from Web Applications - providing that they do have extended permissions. Surprisingly enough, we can even make a Web Application recycle it's very own app pool!
Here's the code snippet we can use to achieve such result:
/// Recycles a web site Application Pool (including the current web site).
/// Requires to reference Microsoft.Web.Administration and System.Web.Hosting.
/// IMPORTANT: The IIS user requires extended permissions to recycle application pool(s).
/// <param name="siteName">The site name: leave it NULL to recycle the current site's App Pool.</param>
/// <returns>TRUE if the site's App Pool has been recycled; FALSE if no site has been found with the given name.</returns>
public static bool RecycleApplicationPool(string siteName = null)
// EDIT: thanks to Emir Krkic for the suggestion below!
// ref.: https://www.ryadel.com/en/asp-net-c-recycle-net-web-applications-app-pool-programmatically-iis/#comment-1552
// if (siteName == null) siteName = HostingEnvironment.ApplicationHost.GetSiteName();
if (siteName == null) siteName = System.Web.Hosting.HostingEnvironment.SiteName;
using (ServerManager iisManager = new ServerManager())
SiteCollection sites = iisManager.Sites;
foreach (Site site in sites)
if (site.Name == siteName)
I took this code from this StackOverflow thread and slightly modified it so that I could use that to recycle any Application Pool available.
As the code comment suggests, we will need to add a reference to the Microsoft.Web.Administration and System.Web.Hosting namespaces. In order to do that, we need to use the Add Reference... feature of our Visual Studio project and add an explicit reference to the Microsoft.Web.Administration assembly:
That's about it: happy recycling!
9 Comments on “ASP.NET C# - How to Recycle a .NET Web Application's App Pool programmatically”
You should not use the HostingEnvironment.ApplicationHost.GetSiteName() directly. This is stated in the documentation in MSDN. https://docs.microsoft.com/en-us/dotnet/api/system.web.hosting.iapplicationhost.getsitename?redirectedfrom=MSDN&view=netframework-4.8#System_Web_Hosting_IApplicationHost_GetSiteName
Use: System.Web.Hosting.HostingEnvironment.SiteName instead.
Fixed: thanks for the suggestion! I’ve mentioned you in the sample code as well.
This is awesome. I am really looking forward to using it. However, sites always seem to come back null for me. I am at a loss.
make sure you are adding the reference to the correct Microsoft.Web.Administration – the IIS one should be located under c:\windows\system32\inetsrv\
It might be that you added a reference to other Microsoft.Web.Administration assemblies, such as the one for IIS Express, which would obviously give different behaviours.
Also, you can explicitly set the proper applicationHost.config path by doing this:
ServerManager iisManager = new ServerManager(@”C:\Windows\System32\inetsrv\config\applicationHost.config”);
Please let us know if you manage do fix your issue.
How to allow IIS user to recycle application pool
Well, you could make a button that will postback to a controller that will fire that… Be wary that the user will get disconnected and therefore won’t receive the response, though.
I was wondering if this can be prevented by my hosting provider
Great article, thank you very much!
What permissions does the IIS User need? And where do I change those?