问题描述
我有一些使用 Microsoft.Web.Administration.ServerManager
的实用程序方法,我遇到了一些问题.出于说明目的,请使用以下无效的简单代码.
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);
}
}
如果我将该代码直接放在控制台应用程序中并运行它,它将获取并列出 IIS express 网站.如果我从提升的命令提示符运行该应用程序,它将列出 IIS7 网站.有点不方便,但到目前为止还不错.
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.
如果我将该代码放在控制台应用程序引用和调用的类库中,它总是会列出 IIS Express 站点,即使控制台应用程序已提升.
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 引导我尝试以下方法,但没有成功.
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%system32inetsrvconfigapplicationhost.config");
显然我误解了高高在上"的方式.进程运行.在提升的进程中执行的所有内容,甚至来自另一个 dll 的代码,不应该以提升的权限运行吗?显然不是?
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?
感谢您的帮助!
推荐答案
确保添加对正确 Microsoft.Web.Administration 的引用,应该是位于 c:windowssystem32 下的 v7.0.0.0inetsrv看起来您正在添加对 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:windowssystem32inetsrv It looks like you are adding a reference to IIS Express's Microsoft.Web.Administraiton which will give you that behavior
这篇关于如何使用 ServerManager 从类库中读取 IIS 站点,而不是 IIS express,或者提升的进程如何处理类库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!