如何使用 ServerManager 从类库读取 IIS 站点,而不是 IIS Express,或者提升的进程如何处理类库?
我有一些使用 Microsoft.Web.Administration.ServerManager
的实用程序方法,但我一直遇到一些问题。使用以下极其简单的代码用于说明目的。
using(var mgr = new ServerManager())
{
foreach(var site in mgr.Sites)
{
Console.WriteLine(site.Name);
}
}
如果我将该代码直接放入控制台应用程序中并运行它,它将获取并列出 IIS Express 网站。如果我从提升的命令提示符运行该应用程序,它将列出 IIS7 网站。有点不方便,但到目前为止还不错。
如果我将该代码放入由控制台应用程序引用和调用的类库中,它将始终列出 IIS Express 站点,即使控制台应用程序已提升。
谷歌引导我尝试以下方法,但没有成功。
//This returns IIS express
var mgr = new ServerManager();
//This returns IIS express
var mgr = ServerManager.OpenRemote(Environment.MachineName);
//This throws an exception
var mgr = new ServerManager(@"%windir%\system32\inetsrv\config\applicationhost.config");
显然我误解了“提升”流程的运行方式。难道在提升的进程中执行的所有内容(甚至来自另一个 dll 的代码)不应该以提升的权限运行吗?显然不是?
感谢您的帮助!
I have some utility methods that use Microsoft.Web.Administration.ServerManager
that I've been having some issues with. Use the following dead simple code for illustration purposes.
using(var mgr = new ServerManager())
{
foreach(var site in mgr.Sites)
{
Console.WriteLine(site.Name);
}
}
If I put that code directly in a console application and run it, it will get and list the IIS express websites. If I run that app from an elevated command prompt, it will list the IIS7 websites. A little inconvenient, but so far so good.
If instead I put that code in a class library that is referenced and called by the console app, it will ALWAYS list the IIS Express sites, even if the console app is elevated.
Google has led me to try the following, with no luck.
//This returns IIS express
var mgr = new ServerManager();
//This returns IIS express
var mgr = ServerManager.OpenRemote(Environment.MachineName);
//This throws an exception
var mgr = new ServerManager(@"%windir%\system32\inetsrv\config\applicationhost.config");
Evidently I've misunderstood something in the way an "elevated" process runs. Shouldn't everything executing in an elevated process, even code from another dll, be run with elevated rights? Evidently not?
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
确保添加对正确 Microsoft.Web.Administration 的引用,应为位于 c:\windows\system32\inetsrv\ 下的 v7.0.0.0
看起来您正在添加对 IIS Express 的 Microsoft.Web.Administraiton 的引用,这将为您提供该行为
Make sure you are adding the reference to the correct Microsoft.Web.Administration, should be v7.0.0.0 that is located under c:\windows\system32\inetsrv\
It looks like you are adding a reference to IIS Express's Microsoft.Web.Administraiton which will give you that behavior
您的问题帮助我找到了 PowerShell 的答案,因此如果互联网正在搜索如何做到这一点:
Your question helped me find the answer for PowerShell, so if the Internet is searching for how to do that:
小心!使用这种方法,我们发现了看似随机的问题,例如“不支持的操作”异常、无法添加/删除 HTTPS 绑定、在 IIS Express 中运行时无法启动/停止应用程序池以及其他问题。目前尚不清楚这是由于 IIS 通常存在错误还是由于此处描述的非正统方法所致。总的来说,我的印象是所有用于自动化 IIS 的工具(appcmd、Microsoft.Web.Administration、PowerShell 等)都不稳定且不稳定,尤其是在不同的操作系统版本之间。良好的测试(一如既往)是可取的!
编辑:另请参阅此答案的评论,了解为什么此方法可能不稳定。
常规
Microsoft.Web.Administration<从 NuGet 安装的 /code> 包工作正常。无需复制任何系统 DLL。
官方文档中的明显解决方案也可以正常工作:
即使您从 IIS Express 的应用程序池中执行上述内容,该解决方案也可以正常工作。您仍然会看到“真实”IIS 的配置。您甚至可以添加新站点,只要您的应用程序以有权执行此操作的用户身份运行即可。
但请注意,上面的构造函数被记录为“仅限 Microsoft 内部使用”:
https://msdn.microsoft.com/en-us/library/ms617371(v=vs.90).aspx
CAUTION! Using this approach we have seen seemingly random issues such as "unsupported operation" exceptions, failure to add/remove HTTPS bindings, failure to start/stop application pools when running in IIS Express, and other problems. It is unknown whether this is due to IIS being generally buggy or due to the unorthodox approach described here. In general, my impression is that all tools for automating IIS (appcmd, Microsoft.Web.Administration, PowerShell, ...) are wonky and unstable, especially across different OS versions. Good testing is (as always) advisable!
EDIT: Please also see the comments on this answer as to why this approach may be unstable.
The regular
Microsoft.Web.Administration
package installed from NuGet works fine. No need to copy any system DLLs.The obvious solution from the official documentation also works fine:
This works even if you execute the above from within the application pool of IIS Express. You will still see the configuration of the "real" IIS. You will even be able to add new sites, as long as your application runs as a user with permission to do so.
Note, however that the constructor above is documented as "Microsoft internal use only":
https://msdn.microsoft.com/en-us/library/ms617371(v=vs.90).aspx
这非常有效。无需更改任何引用
This works perfectly. No need to change any references