以编程方式从 Windows 2003 服务器控制 IIS 7 服务器

发布于 2024-09-14 09:14:51 字数 1804 浏览 2 评论 0原文

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

这是我们用来发送 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 不,有更好的方法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水晶透心 2024-09-21 09:14:51

从周围的阅读来看,似乎不可能做你正在寻找的事情。仅仅包含 dll 文件是不够的。
根据 http://forums.iis.net/t/1149274.aspx..

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

不幸的是,没有 SDK 可以实现此功能,并且它对其他组件有多个依赖项,这些组件不允许您将其带到另一台计算机上并使其工作(例如 COM 对象、DLL 等)。

我很想知道你是否找到了解决这个问题的方法。

谢谢

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

長街聽風 2024-09-21 09:14:51

尝试使用 DirectoryEntry 控制 IIS 池。

请参阅此主题:
检查应用程序池的状态 (IIS 6 ) 使用 C#

Try controlling the IIS pool with DirectoryEntry instead.

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

不顾 2024-09-21 09:14:51

Microsoft.Web.Administration,它依赖于框架4提供的System.Web.dll,而不是客户端配置文件。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文