Click here to Skip to main content
15,126,317 members
Articles / Programming Languages / C#
Posted 2 Jun 2014

Tagged as


14 bookmarked

Recycle Application Pool(s) in IIS 7 Server Using C# Code

Rate me:
Please Sign up or sign in to vote.
4.86/5 (5 votes)
2 Jun 2014CPOL2 min read
In this tip, we will try to understand how to stop, start and recycle the application pools using simple C# code


The need for programmatically executing an IIS application pool recycle can arise for a number of reasons. It happened to me when I was developing a WF (Windows workflow) activity to deploy the code to IIS virtual directory where I force an app pool recycle after deployment. After doing a lot of search, I found the API (Microsoft.Web.Administration.dll) that provides most of the features like start, stop, recycle functions, etc.

Namespace: Microsoft.Web.Administration

The API can be found in %WinDir%\system32\Inetsrv\Microsoft.Web.Administration.dll.

Assembly: Microsoft.Web.Administration (in Microsoft.Web.Administration.dll)

Namespace: Microsoft.Web.Administration

The Microsoft.Web.Administration namespace consist of classes that can be used to administer IIS. This API provides lots of functionality like read and write configuration information of ApplicationHost.config, Web.config, and Administration.config files.

Also API can be used for creating a site, starting or stopping a site, deleting an application pool, recycle, start and stop an application pool, etc.

Add Reference with the Help of Nuget

You can use the below command to install the Microsoft.Web.Administration DLL reference with the help of nugget package console.

Install-Package Microsoft.Web.Administration

Recycle All Available Pools

API provides the following main classes to play with the IIS app pools.

  1. ServerManager
  2. ApplicationPoolCollection
  3. ApplicationPool

Following are the steps that we need to follow to recycle all available pools in IIS.

Step 1. Add the Microsoft.Web.Administration DLL to your class from %WinDir%\system32\Inetsrv\Microsoft.Web.Administration.dll or through nuget (as mentioned above).

Step 2. Create a function to where you are going to recycle all app pools. Now use the below mentioned code to get the app pools and recycle them one by one.

Server Manager class initializes the new instance by using the default path of the ApplicationHost.config file.

In the same way, you can use the start and stop functions.

public static void RecycleAppPools()
    ServerManager serverManager = new ServerManager();
    ApplicationPoolCollection appPools = serverManager.ApplicationPools;
    foreach (ApplicationPool ap in appPools)

Recycle a Particular Pool

Follow the same steps given above. Only difference here is that instead of getting all the app pools, get the specified pool from the app pool collection as shown in the below code:

public static void RecycleAppPool()
      ServerManager serverManager = new ServerManager();
      ApplicationPool appPool = serverManager.ApplicationPools["Test_AppPool"];
      if (appPool != null)
          if (appPool.State == ObjectState.Stopped)


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

QuestionThree years old and still valid Pin
Magius9627-Feb-17 6:51
MemberMagius9627-Feb-17 6:51 
QuestionRecycle Method is not implemented Pin
Member 1247111319-Oct-16 23:15
MemberMember 1247111319-Oct-16 23:15 
QuestionThe method or operation is not implemented. Pin
Member 1280421719-Oct-16 22:04
MemberMember 1280421719-Oct-16 22:04 
SuggestionSolve the permission issue in local machine. Pin
Lourenço Camillo MIlhati10-Jun-15 9:18
MemberLourenço Camillo MIlhati10-Jun-15 9:18 
Questionsolution for Error when using Microsoft.Web.Administration; Pin
rajmeerk22-Apr-15 0:02
Memberrajmeerk22-Apr-15 0:02 
AnswerRe: solution for Error when using Microsoft.Web.Administration; Pin
Member 1306473815-May-18 21:52
MemberMember 1306473815-May-18 21:52 
QuestionEcxeption!!!!!! Pin
ma.compengin5-Jan-15 1:33
Memberma.compengin5-Jan-15 1:33 
QuestionAuth Error Pin
Maykol Porto Rypka16-Dec-14 5:49
MemberMaykol Porto Rypka16-Dec-14 5:49 
Question5! Pin
Volynsky Alex3-Jun-14 9:12
professionalVolynsky Alex3-Jun-14 9:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.