ASP.NET C# - How to Recycle a .NET Web Application's App Pool programmatically

ASP.NET C# - How to Recycle a .NET Web Application's App Pool programmatically

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 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:

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:

ASP.NET C# - How to Recycle a .NET Web Application's App Pool programmatically

That's about it: happy recycling!


About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

9 Comments on “ASP.NET C# - How to Recycle a .NET Web Application's App Pool programmatically”

  1. You should not use the HostingEnvironment.ApplicationHost.GetSiteName() directly. This is stated in the documentation in MSDN.

    Use: System.Web.Hosting.HostingEnvironment.SiteName instead.

  2. 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.

    1. Hi Robert,

      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.

    1. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.