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,或者提升的进程如何处理类库?)
问题描述
我有一些使用 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,或者提升的进程如何处理类库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 ServerManager 从类库中读取 IIS 站点,而不是 IIS express,或者提升的进程如何处理类库?
基础教程推荐
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- rabbitmq 的 REST API 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01