Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#
Tip/Trick

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 66.7K   14   9
In this tip, we will try to understand how to stop, start and recycle the application pools using simple C# code

Introduction

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.

C#
public static void RecycleAppPools()
{
    ServerManager serverManager = new ServerManager();
    ApplicationPoolCollection appPools = serverManager.ApplicationPools;
    foreach (ApplicationPool ap in appPools)
    {
        ap.Recycle();
    }
}

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:

C#
public static void RecycleAppPool()
  {
      ServerManager serverManager = new ServerManager();
      ApplicationPool appPool = serverManager.ApplicationPools["Test_AppPool"];
      if (appPool != null)
      {
          if (appPool.State == ObjectState.Stopped)
          {
              appPool.Start();
          }
          else
          {
              appPool.Recycle();
          }
      }
  }

License

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


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThree years old and still valid Pin
Magius9627-Feb-17 5:51
Magius9627-Feb-17 5:51 
QuestionRecycle Method is not implemented Pin
Member 1247111319-Oct-16 22:15
Member 1247111319-Oct-16 22:15 
QuestionThe method or operation is not implemented. Pin
Member 1280421719-Oct-16 21:04
Member 1280421719-Oct-16 21:04 
SuggestionSolve the permission issue in local machine. Pin
Lourenço Camillo MIlhati10-Jun-15 8:18
Lourenço Camillo MIlhati10-Jun-15 8:18 
Questionsolution for Error when using Microsoft.Web.Administration; Pin
rajmeerk21-Apr-15 23:02
rajmeerk21-Apr-15 23:02 
AnswerRe: solution for Error when using Microsoft.Web.Administration; Pin
Member 1306473815-May-18 20:52
Member 1306473815-May-18 20:52 
QuestionEcxeption!!!!!! Pin
ma.compengin5-Jan-15 0:33
ma.compengin5-Jan-15 0:33 
QuestionAuth Error Pin
Maykol Porto Rypka16-Dec-14 4:49
Maykol Porto Rypka16-Dec-14 4:49 
Question5! Pin
Volynsky Alex3-Jun-14 8:12
professionalVolynsky Alex3-Jun-14 8: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.