Control IIS 7 server from Windows 2003 server programmatically

1

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?
c#
iis
iis-7
windows-server-2008
windows-server-2003
asked on Stack Overflow Aug 17, 2010 by david.mchonechase • edited Aug 19, 2010 by david.mchonechase

3 Answers

2

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

answered on Stack Overflow Dec 2, 2010 by ShaneH
0

Try controlling the IIS pool with DirectoryEntry instead.

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

answered on Stack Overflow Dec 16, 2010 by rodrigogq • edited May 23, 2017 by Community
0

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

answered on Stack Overflow Sep 13, 2012 by suiyingming • edited Sep 25, 2012 by Austin Henley

User contributions licensed under CC BY-SA 3.0