2010-08-17 79 views
1

我們使用Windows 2003服務器運行各種作業。其中一些作業將應用程序池命令發送到運行IIS 6的Web服務器(回收,啓動,停止)。現在我們有一臺運行IIS 7的Windows 2008 Web服務器,並且我們想發送相同的命令。這全部使用C#完成。以編程方式從Windows 2003服務器控制IIS 7服務器

這是我們用來發送命令爲IIS 6的代碼:

var methodToInvoke = "Stop"; // could be "Stop", "Start", or "Recycle" 
var co = new ConnectionOptions 
{ 
    Impersonation = ImpersonationLevel.Impersonate, 
    Authentication = AuthenticationLevel.PacketPrivacy 
}; 

var objPath = string.Format("IISApplicationPool.Name='W3SVC/AppPools/{0}'", appPoolName); 
var scope = new ManagementScope(string.Format(@"\\{0}\root\MicrosoftIISV2", machineName), co); 

using (var mc = new ManagementObject(objPath)) 
{ 
    mc.Scope = scope; 
    mc.InvokeMethod(methodToInvoke, null, null); 
} 

此代碼不能用於IIS 7,由於潛在的變化工作,所以我們目前正在嘗試這樣的:

using (ServerManager serverManager = ServerManager.OpenRemote(machineName)) 
{ 
    var appPool = serverManager.ApplicationPools[appPoolName]; 
    if (appPool != null) 
    { 
     appPool.Stop(); // or app.Start() or app.Recycle() 
     serverManager.CommitChanges(); 
    } 
} 

上述代碼在我的工作站上運行良好,該工作站運行Windows 7(以及IIS 7.5)。但是,當我將這段代碼部署到我們的應用程序服務器時,它不起作用。它得到這個錯誤:

System.InvalidCastException: 
Unable to cast COM object of type 'System.__ComObject' to interface type 
'Microsoft.Web.Administration.Interop.IAppHostWritableAdminManager'. 
This operation failed because the QueryInterface call on the COM component for the 
interface with IID '{FA7660F6-7B3F-4237-A8BF-ED0AD0DCBBD9}' failed due to the following error: 
Interface not registered (Exception from HRESULT: 0x80040155). 

從我的研究,這是由於IIS 7在Windows Server 2003服務器上不可用。 (我沒有包括Microsoft.Web.Administration.dll文件。)

所以我的問題是:

  1. 是否有可能爲IIS 7上面的代碼在所有從Windows 2003服務器工作?
  2. 如果不是#1,有沒有更好的方法來做到這一點?

回答

2

從周圍看它似乎不可能做你正在尋找的東西。包括dll文件是不夠的。 據http://forums.iis.net/t/1149274.aspx ..

In order to use Microsoft.Web.Administration you need to have IIS installed, at the bare minimum you need to install the Configuration API's which are brought through installing the Management Tools.

Unfortunately there is no SDK that enables this and it has several dependencies on other components that wouldn't let you just take it to another machine and make it work (such as COM objects, DLL's, etc).

我會想知道,如果你已經找到一個辦法解決這。

謝謝

+0

我最終在Web服務器本身上創建了一個Web服務,它將啓動/停止/回收該服務器上的應用程序池。這個Web服務顯然需要位於一個單獨的應用程序池上,而不是它將更新的應用程序池。這不是一個非常安全的解決方案,但我正在使用內部Web服務器。 – 2010-12-07 14:23:50

+0

出現同樣的問題,您的文章確認了我害怕的 - 管理API在Windows XP上無法正常工作。感謝帖子 – Jeremy 2011-09-14 17:57:30

0

Microsoft.Web.Administration,它依賴於System.Web.dll這是由框架4,而不是客戶端配置文件提供。

+1

這不回答這個問題。 – 2012-09-25 02:28:19