如何通过ASP.Net和C#回收IIS 6中的应用程序池?

时间:2012-02-06 12:08:26

标签: c# asp.net asp.net-mvc iis iis-6

我有一个IIS 6服务器,我需要回收一个特定的应用程序池。我需要使用C#设计一个执行此任务的ASP.NET网页。

我该怎么做?

4 个答案:

答案 0 :(得分:2)

只需制作一个单独的网页/网络应用程序,然后将其安装在定位到另一个应用程序池的Web服务器上(如果在应用程序的同一页面上运行并链接到要回收的同一个应用程序池,则不确定它是如何工作的)。

然后按照此处的说明操作:https://stackoverflow.com/a/496357/559144

答案 1 :(得分:0)

您可以使用DirectoryEntry类以编程方式回收给定名称的应用程序池:

var path = "IIS://localhost/W3SVC/AppPools/MyAppPool";
var appPool = new DirectoryEntry(path);
appPool.Invoke("Recycle");

答案 2 :(得分:0)

以下应该(我不能证明代码在相当长的一段时间内没有使用)足够了:

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");
    }
}

答案 3 :(得分:0)

我使用这种方法。

HttpRuntime.UnloadAppDomain()

HttpRuntime.UnloadAppDomain Method (System.Web)