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

How to use ServerManager to read IIS sites, not IIS express, from class library OR how do elevated processes handle class libraries?(如何使用 ServerManager 从类库中读取 IIS 站点,而不是 IIS express,或者提升的进程如何处理类库?)
本文介绍了如何使用 ServerManager 从类库中读取 IIS 站点,而不是 IIS express,或者提升的进程如何处理类库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些使用 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,或者提升的进程如何处理类库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)