以编程方式从 Windows 2003 服务器控制 IIS 7 服务器
我们使用 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 文件。)
所以我的问题是:
- 上述 IIS 7 代码是否可以在 Windows 2003 服务器上工作?
- 如果#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:
- Is it possible for the above code for IIS 7 to work at all from a Windows 2003 server?
- If no to #1, is there a better way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从周围的阅读来看,似乎不可能做你正在寻找的事情。仅仅包含 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..
I'd be interested in knowing if you've found a way round this.
Thanks
尝试使用 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#
Microsoft.Web.Administration
,它依赖于框架4提供的System.Web.dll
,而不是客户端配置文件。Microsoft.Web.Administration
, it relies onSystem.Web.dll
which was provided by framework 4, not client profile.