1

I have an IIS 6 server and I need to recycle a specific application pool. I need to design an ASP.NET web page using C# that performs this task.

How can I do this?

Salman A
  • 262,204
  • 82
  • 430
  • 521
Riyaju
  • 67
  • 1
  • 8

4 Answers4

2

just make a separated web page / web application and install it on the web server targeting another app pool (not sure how it would work if running as same page of your app and being linked to the same app pool you want to recycle).

then follow instructions here: https://stackoverflow.com/a/496357/559144

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
0

You can use the DirectoryEntry class to programmatically recycle an application pool given its name:

var path = "IIS://localhost/W3SVC/AppPools/MyAppPool";
var appPool = new DirectoryEntry(path);
appPool.Invoke("Recycle");
Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
0

The following should (I can't attest to that as the code hasn't been used in quite some time) suffice:

using System;
using System.Collections.Generic;
using System.Web;
using System.DirectoryServices;

public static class ApplicationPoolRecycle
{
    public static void RecycleCurrentApplicationPool()
    {
        string appPoolId = GetCurrentApplicationPoolId();
        RecycleApplicationPool(appPoolId);
    }
    private static string GetCurrentApplicationPoolId()
    {
        string virtualDirPath = AppDomain.CurrentDomain.FriendlyName;
        virtualDirPath = virtualDirPath.Substring(4);
        int index = virtualDirPath.Length + 1;
        index = virtualDirPath.LastIndexOf("-", index - 1, index - 1);
        index = virtualDirPath.LastIndexOf("-", index - 1, index - 1);
        virtualDirPath = "IIS://localhost/" + virtualDirPath.Remove(index);
        DirectoryEntry virtualDirEntry = new DirectoryEntry(virtualDirPath);
        return virtualDirEntry.Properties["AppPoolId"].Value.ToString();
    }
    private static void RecycleApplicationPool(string appPoolId)
    {
        string appPoolPath = "IIS://localhost/W3SVC/AppPools/" + appPoolId;
        DirectoryEntry appPoolEntry = new DirectoryEntry(appPoolPath);
        appPoolEntry.Invoke("Recycle");
    }
}
Rob
  • 45,296
  • 24
  • 122
  • 150
  • How Can I design in Aspx Form?? – Riyaju Feb 06 '12 at 12:51
  • @Riyaju - you just need a page that calls `ApplicationPoolRecycle.RecycleCurrentApplicationPool()` either as a result of calling the page (i.e. in `Page_Load`) or as a result of clicking on a button (i.e. `myButton_Click`) – Rob Feb 06 '12 at 12:54
0

I using this method.

HttpRuntime.UnloadAppDomain()

HttpRuntime.UnloadAppDomain Method (System.Web)

takepara
  • 10,413
  • 3
  • 34
  • 31