以编程方式从Windows 2003服务器控制IIS 7服务器 [英] Control IIS 7 server from Windows 2003 server programmatically

查看:229
本文介绍了以编程方式从Windows 2003服务器控制IIS 7服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用Windows 2003服务器运行各种作业。其中一些作业将应用程序池命令发送到运行IIS 6的Web服务器(循环,启动,停止)。现在我们有一个运行IIS 7的Windows 2008 Web服务器,我们想发送相同的命令。这都是使用C#完成的。

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

这是我们用于发送IIS 6命令的代码:

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);
}

由于基础更改,此代码不适用于IIS 7,因此我们'目前正在尝试这个:

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();
   }
}

以上代码在我的工作站上正常运行Windows 7(以及IIS 7.5)。但是,当我将此代码部署到我们的应用程序服务器时,它不起作用。它收到此错误:

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

根据我的研究,这是因为IIS 7在Windows Server 2003上不可用服务器。 (我确实包含了Microsoft.Web.Administration.dll文件。)

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

所以我的问题是:


  1. 上面的IIS 7代码是否可以在Windows 2003服务器上运行?

  2. 如果不是#1,是否有更好的方法这样做?


推荐答案

从阅读中看似乎不可能做什么您正在寻找。包含dll文件是不够的。
根据 http://forums.iis.net/t/1149274.aspx ..

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


为了使用Microsoft.Web.Administration,您需要安装IIS,至少需要安装通过安装管理工具带来的配置API。

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.

不幸的是,没有SDK可以实现这一点,并且它对其他组件有几个依赖性,不会让你这么做将它带到另一台机器并使其工作(如COM对象,DLL等)。

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.

谢谢

这篇关于以编程方式从Windows 2003服务器控制IIS 7服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆