Click here to Skip to main content
Click here to Skip to main content

Tagged as

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

, 2 Jun 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

        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:

      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)

Share

About the Author

Er. HARENDRA KUMAR
Software Developer (Senior)
India India
No Biography provided

Comments and Discussions

 
QuestionAuth Error PinmemberMaykol Porto Rypka16-Dec-14 5:49 
Question5! PinprofessionalVolynsky Alex3-Jun-14 9:12 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 2 Jun 2014
Article Copyright 2014 by Er. HARENDRA KUMAR
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid