Restarting (Recycling) an Application Pool
Asked Answered
L

12

75

How can I restart(recycle) IIS Application Pool from C# (.net 2)?

Appreciate if you post sample code?

Locke answered 30/10, 2008 at 11:54 Comment(0)
C
62

If you're on IIS7 then this will do it if it is stopped. I assume you can adjust for restarting without having to be shown.

// Gets the application pool collection from the server.
[ModuleServiceMethod(PassThrough = true)]
public ArrayList GetApplicationPoolCollection()
{
    // Use an ArrayList to transfer objects to the client.
    ArrayList arrayOfApplicationBags = new ArrayList();

    ServerManager serverManager = new ServerManager();
    ApplicationPoolCollection applicationPoolCollection = serverManager.ApplicationPools;
    foreach (ApplicationPool applicationPool in applicationPoolCollection)
    {
        PropertyBag applicationPoolBag = new PropertyBag();
        applicationPoolBag[ServerManagerDemoGlobals.ApplicationPoolArray] = applicationPool;
        arrayOfApplicationBags.Add(applicationPoolBag);
        // If the applicationPool is stopped, restart it.
        if (applicationPool.State == ObjectState.Stopped)
        {
            applicationPool.Start();
        }

    }

    // CommitChanges to persist the changes to the ApplicationHost.config.
    serverManager.CommitChanges();
    return arrayOfApplicationBags;
}

If you're on IIS6 I'm not so sure, but you could try getting the web.config and editing the modified date or something. Once an edit is made to the web.config then the application will restart.

Chaffinch answered 30/10, 2008 at 12:0 Comment(11)
Oh, go ahead and show him how to adjust for restarting. Do you know how to do it?Honour
+1 you are the man. After no less than 10 solutions (including touching web.config), this was the winnar.Stool
Hi, this is a very old post but I am struggling to figure one part out. Where does "ServerManagerDemoGlobals.ApplicationPoolArray" come from? i.e. what should I reference to access it? I have added references to Microsoft.Web.Management.dll and Microsoft.Web.Administration.dll ThanksBraces
@Braces I'm also having that problem.Zeba
@Braces The code seem to originate from here: msdn.microsoft.com/en-us/library/…Zeba
I was able to satisfy most references by adding the NuGet package Microsoft.Web.Administration. I still cannot find the PropertyBag class. MSDN says it's in Microsoft.Web.Management.Server but I can't find that dll anywhere. Anybody know where it is?Theodolite
I added a reference to this and it satisfied the attribute and propertyBag c:\windows\SysWOW64\inetsrv\Microsoft.Web.Management.dll (also found in c:\windows\system32\inetsrv\Microsoft.Web.Management.dll)Theodolite
I'm getting an error at line ApplicationPoolCollection applicationPoolCollection = serverManager.ApplicationPools; Filename: redirection.config Error: Cannot read configuration file due to insufficient permissionsSkittish
@Braces Unless you need to use the ArrayList to pass on your app pool object, just delete the code and don't sweat it. It's only needed because of the example it appears in on MSDN.Fecal
where the hell is ServerManagerDemoGlobals.ApplicationPoolArray??Distribution
@Honour - To adjust that code for restarting (recycle) the application pool, change: applicationPool.Start(); To applicationPool.Recycle(); and remove the if (applicationPool.State == ObjectState.Stopped) condition.Mauldon
P
92

Here we go:

HttpRuntime.UnloadAppDomain();
Prindle answered 4/7, 2009 at 9:45 Comment(6)
That recycles the application, but I'm not sure it recycles the entire app-pool (which can host multiple applications at once).Heid
@Marc - very valid, though sometimes you only care about the current application context. The conditions indicating the need to reload may assert themselves independently in each instance.Prindle
Very helpful, I've been needing this! (I only need it for the current context)Pestle
+1 Im just curious , why would anyone want to recycle his App ? I mean what are the scenarious ( im an asp.net ) developer.Plessor
Very useful if you need to remotely recycle your web app (e.g. long-lived / singleton objects are misbehaving)Leatherwood
@RoyiNamir - a bit of a necro-post but, if one wanted to clear all session data (for all users) this appears to be the only way to do that.Clermontferrand
C
62

If you're on IIS7 then this will do it if it is stopped. I assume you can adjust for restarting without having to be shown.

// Gets the application pool collection from the server.
[ModuleServiceMethod(PassThrough = true)]
public ArrayList GetApplicationPoolCollection()
{
    // Use an ArrayList to transfer objects to the client.
    ArrayList arrayOfApplicationBags = new ArrayList();

    ServerManager serverManager = new ServerManager();
    ApplicationPoolCollection applicationPoolCollection = serverManager.ApplicationPools;
    foreach (ApplicationPool applicationPool in applicationPoolCollection)
    {
        PropertyBag applicationPoolBag = new PropertyBag();
        applicationPoolBag[ServerManagerDemoGlobals.ApplicationPoolArray] = applicationPool;
        arrayOfApplicationBags.Add(applicationPoolBag);
        // If the applicationPool is stopped, restart it.
        if (applicationPool.State == ObjectState.Stopped)
        {
            applicationPool.Start();
        }

    }

    // CommitChanges to persist the changes to the ApplicationHost.config.
    serverManager.CommitChanges();
    return arrayOfApplicationBags;
}

If you're on IIS6 I'm not so sure, but you could try getting the web.config and editing the modified date or something. Once an edit is made to the web.config then the application will restart.

Chaffinch answered 30/10, 2008 at 12:0 Comment(11)
Oh, go ahead and show him how to adjust for restarting. Do you know how to do it?Honour
+1 you are the man. After no less than 10 solutions (including touching web.config), this was the winnar.Stool
Hi, this is a very old post but I am struggling to figure one part out. Where does "ServerManagerDemoGlobals.ApplicationPoolArray" come from? i.e. what should I reference to access it? I have added references to Microsoft.Web.Management.dll and Microsoft.Web.Administration.dll ThanksBraces
@Braces I'm also having that problem.Zeba
@Braces The code seem to originate from here: msdn.microsoft.com/en-us/library/…Zeba
I was able to satisfy most references by adding the NuGet package Microsoft.Web.Administration. I still cannot find the PropertyBag class. MSDN says it's in Microsoft.Web.Management.Server but I can't find that dll anywhere. Anybody know where it is?Theodolite
I added a reference to this and it satisfied the attribute and propertyBag c:\windows\SysWOW64\inetsrv\Microsoft.Web.Management.dll (also found in c:\windows\system32\inetsrv\Microsoft.Web.Management.dll)Theodolite
I'm getting an error at line ApplicationPoolCollection applicationPoolCollection = serverManager.ApplicationPools; Filename: redirection.config Error: Cannot read configuration file due to insufficient permissionsSkittish
@Braces Unless you need to use the ArrayList to pass on your app pool object, just delete the code and don't sweat it. It's only needed because of the example it appears in on MSDN.Fecal
where the hell is ServerManagerDemoGlobals.ApplicationPoolArray??Distribution
@Honour - To adjust that code for restarting (recycle) the application pool, change: applicationPool.Start(); To applicationPool.Recycle(); and remove the if (applicationPool.State == ObjectState.Stopped) condition.Mauldon
C
11

Below method is tested to be working for both IIS7 and IIS8

Step 1 : Add reference to Microsoft.Web.Administration.dll. The file can be found in the path C:\Windows\System32\inetsrv\, or install it as NuGet Package https://www.nuget.org/packages/Microsoft.Web.Administration/

Step 2 : Add the below code

using Microsoft.Web.Administration;

Using Null-Conditional Operator

new ServerManager().ApplicationPools["Your_App_Pool_Name"]?.Recycle();

OR

Using if condition to check for null

var yourAppPool=new ServerManager().ApplicationPools["Your_App_Pool_Name"];
if(yourAppPool!=null)
    yourAppPool.Recycle();
Crucifixion answered 22/11, 2018 at 12:47 Comment(0)
M
8

The code below works on IIS6. Not tested in IIS7.

using System.DirectoryServices;

...

void Recycle(string appPool)
{
    string appPoolPath = "IIS://localhost/W3SVC/AppPools/" + appPool;

    using (DirectoryEntry appPoolEntry = new DirectoryEntry(appPoolPath))
    {
            appPoolEntry.Invoke("Recycle", null);
            appPoolEntry.Close();
    }
}

You can change "Recycle" for "Start" or "Stop" also.

Morrie answered 30/1, 2009 at 17:14 Comment(1)
Note that you need to have "IIS 6 WMI Compatibility" enabled on IIS7Danidania
A
8

I went a slightly different route with my code to recycle the application pool. A few things to note that are different than what others have provided:

1) I used a using statement to ensure proper disposal of the ServerManager object.

2) I am waiting for the application pool to finish starting before stopping it, so that we don't run into any issues with trying to stop the application. Similarly, I am waiting for the app pool to finish stopping before trying to start it.

3) I am forcing the method to accept an actual server name instead of falling back to the local server, because I figured you should probably know what server you are running this against.

4) I decided to start/stop the application as opposed to recycling it, so that I could make sure that we didn't accidentally start an application pool that was stopped for another reason, and to avoid issues with trying to recycle an already stopped application pool.

public static void RecycleApplicationPool(string serverName, string appPoolName)
{
    if (!string.IsNullOrEmpty(serverName) && !string.IsNullOrEmpty(appPoolName))
    {
        try
        {
            using (ServerManager manager = ServerManager.OpenRemote(serverName))
            {
                ApplicationPool appPool = manager.ApplicationPools.FirstOrDefault(ap => ap.Name == appPoolName);

                //Don't bother trying to recycle if we don't have an app pool
                if (appPool != null)
                {
                    //Get the current state of the app pool
                    bool appPoolRunning = appPool.State == ObjectState.Started || appPool.State == ObjectState.Starting;
                    bool appPoolStopped = appPool.State == ObjectState.Stopped || appPool.State == ObjectState.Stopping;

                    //The app pool is running, so stop it first.
                    if (appPoolRunning)
                    {
                        //Wait for the app to finish before trying to stop
                        while (appPool.State == ObjectState.Starting) { System.Threading.Thread.Sleep(1000); }

                        //Stop the app if it isn't already stopped
                        if (appPool.State != ObjectState.Stopped)
                        {
                            appPool.Stop();
                        }
                        appPoolStopped = true;
                    }

                    //Only try restart the app pool if it was running in the first place, because there may be a reason it was not started.
                    if (appPoolStopped && appPoolRunning)
                    {
                        //Wait for the app to finish before trying to start
                        while (appPool.State == ObjectState.Stopping) { System.Threading.Thread.Sleep(1000); }

                        //Start the app
                        appPool.Start();
                    }
                }
                else
                {
                    throw new Exception(string.Format("An Application Pool does not exist with the name {0}.{1}", serverName, appPoolName));
                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception(string.Format("Unable to restart the application pools for {0}.{1}", serverName, appPoolName), ex.InnerException);
        }
    }
}
Afield answered 17/2, 2015 at 1:49 Comment(1)
works good on my iis8, error free just need to add reference Microsoft.Web.Administration as mentioned.Telephoto
A
5

Recycle code working on IIS6:

    /// <summary>
    /// Get a list of available Application Pools
    /// </summary>
    /// <returns></returns>
    public static List<string> HentAppPools() {

        List<string> list = new List<string>();
        DirectoryEntry W3SVC = new DirectoryEntry("IIS://LocalHost/w3svc", "", "");

        foreach (DirectoryEntry Site in W3SVC.Children) {
            if (Site.Name == "AppPools") {
                foreach (DirectoryEntry child in Site.Children) {
                    list.Add(child.Name);
                }
            }
        }
        return list;
    }

    /// <summary>
    /// Recycle an application pool
    /// </summary>
    /// <param name="IIsApplicationPool"></param>
    public static void RecycleAppPool(string IIsApplicationPool) {
        ManagementScope scope = new ManagementScope(@"\\localhost\root\MicrosoftIISv2");
        scope.Connect();
        ManagementObject appPool = new ManagementObject(scope, new ManagementPath("IIsApplicationPool.Name='W3SVC/AppPools/" + IIsApplicationPool + "'"), null);

        appPool.InvokeMethod("Recycle", null, null);
    }
Alliance answered 2/12, 2008 at 8:47 Comment(0)
A
3

Sometimes I feel that simple is best. And while I suggest that one adapts the actual path in some clever way to work on a wider way on other enviorments - my solution looks something like:

ExecuteDosCommand(@"c:\Windows\System32\inetsrv\appcmd recycle apppool " + appPool);

From C#, run a DOS command that does the trick. Many of the solutions above does not work on various settings and/or require features on Windows to be turned on (depending on setting).

Alphabetize answered 3/9, 2013 at 6:35 Comment(4)
This especially if you have Unknown Error 0x80005000 from trying one of the other solutions. That are all good in their own way.Alphabetize
I recommend this on how to make your own 'ExecuteDosCommand' method. codeproject.com/Articles/25983/How-to-Execute-a-Command-in-CAlphabetize
How would you run the above command if you are running it on a remote server?Atwekk
This one worked the best for me. Except that I made 2 calls: stop and start instead on recycleTrisomic
N
3

this code work for me. just call it to reload application.

System.Web.HttpRuntime.UnloadAppDomain()
Narine answered 8/12, 2015 at 11:4 Comment(0)
I
1

Here is a simple solution if you just want to recycle all the app pools on the current machine. I had to "run as administrator" for this to work.

using (var serverManager = new ServerManager())
{
    foreach (var appPool in serverManager.ApplicationPools)
    {
        appPool.Recycle();
    }
}
Ineptitude answered 27/4, 2022 at 18:45 Comment(0)
P
0

Another option:

System.Web.Hosting.HostingEnvironment.InitiateShutdown();

Seems better than UploadAppDomain which "terminates" the app while the former waits for stuff to finish its work.

Poverty answered 17/7, 2019 at 14:12 Comment(0)
A
0

I found an easy way. Just append or trim text at web.config.

var webConfigFilePath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "web.config");

var fileText = File.ReadAllText(webConfigFilePath);
if (fileText.EndsWith(Environment.NewLine))
{
    File.WriteAllText(webConfigFilePath, fileText.Trim());
}
else
{
    using StreamWriter sw = File.AppendText(webConfigFilePath);
    sw.WriteLine("");
}
Aristippus answered 17/7, 2024 at 2:46 Comment(1)
Welcome to SO. Could you kindly explain how to recycle the pool with only one empty line?Izaak

© 2022 - 2025 — McMap. All rights reserved.