views:

28

answers:

0

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?