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

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

问题描述:

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

我该怎么做?

可以使用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"); 
    } 
} 
+0

我如何在.aspx的外形设计? – Riyaju 2012-02-06 12:51:11

+0

@Riyaju - 您只需要一个调用ApplicationPoolRecycle.RecycleCurrentApplicationPool()的页面,该页面可以是调用页面的结果(即在Page_Load中),也可以是点击按钮(即myButton_Click)的结果。 – Rob 2012-02-06 12:54:45

只是做一个分离,网页/ Web应用程序和Web服务器瞄准另一个应用程序池(不知道运行你的应用程序作为同一页面和被链接到它如何工作在安装相同的应用程序池,你想回收)。

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

我用这个方法。

HttpRuntime.UnloadAppDomain()

HttpRuntime.UnloadAppDomain Method (System.Web)