如何使用 ServerManager 从类库读取 IIS 站点,而不是 IIS Express,或者提升的进程如何处理类库?

发布于 2024-12-20 11:13:28 字数 838 浏览 3 评论 0原文

我有一些使用 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 技术交流群。

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

发布评论

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

评论(4

纸伞微斜 2024-12-27 11:13:28

确保添加对正确 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

巴黎盛开的樱花 2024-12-27 11:13:28

您的问题帮助我找到了 PowerShell 的答案,因此如果互联网正在搜索如何做到这一点:

$assembly = [System.Reflection.Assembly]::LoadFrom("$env:systemroot\system32\inetsrv\Microsoft.Web.Administration.dll")

# load IIS express
$iis = new-object Microsoft.Web.Administration.ServerManager 
$iis.Sites

# load IIS proper
$iis = new-object Microsoft.Web.Administration.ServerManager "$env:systemroot\system32\inetsrv\config\applicationhost.config"  
$iis.Sites

Your question helped me find the answer for PowerShell, so if the Internet is searching for how to do that:

$assembly = [System.Reflection.Assembly]::LoadFrom("$env:systemroot\system32\inetsrv\Microsoft.Web.Administration.dll")

# load IIS express
$iis = new-object Microsoft.Web.Administration.ServerManager 
$iis.Sites

# load IIS proper
$iis = new-object Microsoft.Web.Administration.ServerManager "$env:systemroot\system32\inetsrv\config\applicationhost.config"  
$iis.Sites
初见终念 2024-12-27 11:13:28

小心!使用这种方法,我们发现了看似随机的问题,例如“不支持的操作”异常、无法添加/删除 HTTPS 绑定、在 IIS Express 中运行时无法启动/停止应用程序池以及其他问题。目前尚不清楚这是由于 IIS 通常存在错误还是由于此处描述的非正统方法所致。总的来说,我的印象是所有用于自动化 IIS 的工具(appcmd、Microsoft.Web.Administration、PowerShell 等)都不稳定且不稳定,尤其是在不同的操作系统版本之间。良好的测试(一如既往)是可取的!

编辑:另请参阅此答案的评论,了解为什么此方法可能不稳定。

常规 Microsoft.Web.Administration<从 NuGet 安装的 /code> 包工作正常。无需复制任何系统 DLL。

官方文档中的明显解决方案也可以正常工作:

ServerManager iisManager = new ServerManager(Environment.SystemDirectory + @"inetsrv\config\applicationHost.config");

即使您从 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:

ServerManager iisManager = new ServerManager(Environment.SystemDirectory + @"inetsrv\config\applicationHost.config");

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

〆凄凉。 2024-12-27 11:13:28
var iisManager = new ServerManager(Environment.SystemDirectory + "\\inetsrv\\config\\applicationhost.config");

这非常有效。无需更改任何引用

var iisManager = new ServerManager(Environment.SystemDirectory + "\\inetsrv\\config\\applicationhost.config");

This works perfectly. No need to change any references

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