【发布时间】:2012-02-06 12:08:25
【问题描述】:
我有一个 IIS 6 服务器,我需要回收一个特定的应用程序池。我需要使用 C# 设计一个 ASP.NET 网页来执行此任务。
我该怎么做?
【问题讨论】:
标签: c# asp.net asp.net-mvc iis iis-6
我有一个 IIS 6 服务器,我需要回收一个特定的应用程序池。我需要使用 C# 设计一个 ASP.NET 网页来执行此任务。
我该怎么做?
【问题讨论】:
标签: c# asp.net asp.net-mvc iis iis-6
只需制作一个单独的网页/网络应用程序并将其安装在针对另一个应用程序池的网络服务器上(不确定如果作为应用程序的同一页面运行并链接到您要回收的同一应用程序池它将如何工作)。
然后按照此处的说明进行操作:https://stackoverflow.com/a/496357/559144
【讨论】:
您可以使用DirectoryEntry 类以编程方式回收给定名称的应用程序池:
var path = "IIS://localhost/W3SVC/AppPools/MyAppPool";
var appPool = new DirectoryEntry(path);
appPool.Invoke("Recycle");
【讨论】:
以下应该(我无法证明这一点,因为代码已经有一段时间没有使用了)就足够了:
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");
}
}
【讨论】:
ApplicationPoolRecycle.RecycleCurrentApplicationPool() 的页面,或者作为调用页面的结果(即在Page_Load 中)或作为单击按钮的结果(即myButton_Click )
【讨论】: