Control IIS 7 server from Windows 2003 server programmatically

对着背影说爱祢 提交于 2019-12-11 07:28:00

问题


We run various jobs using a Windows 2003 server. Some of these jobs send app pool commands to web servers running IIS 6 (recycle, start, stop). Now we have a Windows 2008 web server running IIS 7, and we want to send the same commands. This is all done using C#.

This is the code we use to send commands for 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);
}

This code doesn't work for IIS 7 due to underlying changes, so we're currently trying this:

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

The above code works fine on my workstation, which runs Windows 7 (and, thus, IIS 7.5). However, it does not work when I deploy this code to our application server. It get this error:

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).   

From my research, this is due to the fact that IIS 7 is not available on the Windows Server 2003 server. (I did include the Microsoft.Web.Administration.dll file.)

So my questions are:

  1. Is it possible for the above code for IIS 7 to work at all from a Windows 2003 server?
  2. If no to #1, is there a better way of doing this?

回答1:


From reading around it doesn't appear to be possible to do what you're looking for. It's not enough to include the dll files. According to 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).

I'd be interested in knowing if you've found a way round this.

Thanks




回答2:


Try controlling the IIS pool with DirectoryEntry instead.

See this topic: Check the status of an application pool (IIS 6) with C#




回答3:


Microsoft.Web.Administration, it relies on System.Web.dll which was provided by framework 4, not client profile.



来源:https://stackoverflow.com/questions/3504016/control-iis-7-server-from-windows-2003-server-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!