.net 如何引用 Microsoft.Web.Administration?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18104151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reference Microsoft.Web.Administration?
提问by Weyland Yutani
The Microsoft.Web.Administration assembly is found in C:\Windows\System32\inetsrvon my machine. I believe it is installed as part of IIS. The assembly is also in the GAC.
Microsoft.Web.Administration 程序集位于C:\Windows\System32\inetsrv我的机器上。我相信它是作为 IIS 的一部分安装的。该程序集也在 GAC 中。
How should I reference this assembly from my project, given that I want to commit the project to SVN for others to checkout. Microsoft.Web.Administrationdoes not appear in the Visual Studio 'Add References' list. I can add a reference to C:\Windows\System32\inetsrv\Microsoft.Web.Administration, but this seems like a bad idea as other developers might have it installed on a different path or drive.
考虑到我想将项目提交到 SVN 以供其他人签出,我应该如何从我的项目中引用这个程序集。Microsoft.Web.Administration未出现在 Visual Studio 的“添加引用”列表中。我可以添加对 的引用C:\Windows\System32\inetsrv\Microsoft.Web.Administration,但这似乎是个坏主意,因为其他开发人员可能会将其安装在不同的路径或驱动器上。
Or I could copy it into the project folder, but then I have to commit the binary to SVN.
或者我可以将它复制到项目文件夹中,但是我必须将二进制文件提交到 SVN。
回答by Simon
The following steps are working for me:
以下步骤对我有用:
- Go to
C:\Windows\System32\inetsrvand check if the fileMicrosoft.Web.Administration.dllexists. If you are missing theMicrosoft.Web.Administration.dllinC:\Windows\System32\inetsrv, enable IIS Management Console in your Windows Features:
- 转到
C:\Windows\System32\inetsrv并检查文件是否Microsoft.Web.Administration.dll存在。如果您缺少Microsoft.Web.Administration.dllinC:\Windows\System32\inetsrv,请在您的 Windows 功能中启用 IIS 管理控制台:


- In your Visual Studio project, add the reference to the
Microsoft.Web.Administration.dll:
- Now your .proj-file has the following the entry:
<HintPath>C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll</HintPath>. You could adapt this path to e.g.<HintPath>%windir%\System32\inetsrv\Microsoft.Web.Administration.dll</HintPath>to make it more robust.
- 在您的 Visual Studio 项目中,添加对以下内容的引用
Microsoft.Web.Administration.dll:
- 现在您的 .proj 文件具有以下条目:
<HintPath>C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll</HintPath>. 您可以将此路径调整为例如<HintPath>%windir%\System32\inetsrv\Microsoft.Web.Administration.dll</HintPath>使其更健壮。
When you move the app to a different system, the app also references to the Microsoft.Web.Administration.dllon the target system. Because the Microsoft.Web.Administration.dllcould be different between the windows systems it is not recommended to deliver a copy of this assembly file by the app. A copy could be incompatible with the IIS on the target system.
当您将应用程序移动到不同的系统时,该应用程序还会引用Microsoft.Web.Administration.dll目标系统上的 。因为Microsoft.Web.Administration.dllWindows 系统之间可能会有所不同,所以不建议应用程序提供此程序集文件的副本。副本可能与目标系统上的 IIS 不兼容。
It is also not recommended to reference a copy of the assembly on the same system, because perhaps the Microsoft.Web.Administrator.dlland the IIS will change due an Windows Update! Then your referenced copy of Microsoft.Web.Administrator.dllis incompatible with the updated IIS.
也不建议在同一系统上引用程序集的副本,因为Microsoft.Web.Administrator.dllIIS 可能会因 Windows 更新而改变!那么您引用的副本Microsoft.Web.Administrator.dll与更新的 IIS 不兼容。
When the Microsoft.Web.Administrator.dll is missing on the target system, then activate the Management Console as descibed in step 1 above. Alternatively you can use the cmd (open as Administrator) and activate the Windows Feature Management Console by DISM.exevia the following command:
当目标系统上缺少 Microsoft.Web.Administrator.dll 时,请按照上述步骤 1 中的说明激活管理控制台。或者,您可以使用 cmd(以管理员身份打开)并DISM.exe通过以下命令激活 Windows 功能管理控制台:
C:\Windows\System32\Dism.exe /enable-feature /online /featurename:IIS-ManagementConsole
This command could be useful when you want to distribute your app via an setup.
当您想通过设置分发应用程序时,此命令可能很有用。
回答by deve loper
You may modify your project file manually. Adding/Changing the reference like below will find the assembly in GAC regardless of its location:
您可以手动修改您的项目文件。添加/更改如下引用将在 GAC 中找到程序集,无论其位置如何:
<Reference Include="Microsoft.Web.Administration, Version=7.9.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
</Reference>
Of course, if the feature "IIS management console" is installed only. You may simply leave a hint in code:
当然,如果仅安装了“IIS 管理控制台”功能。你可以简单地在代码中留下一个提示:
#warning Windows feature "IIS management console" must be installed locally
回答by Sunny
Search for it in NuGet and install it. It will get installed in packages folder, change the location according to your project structure and re-add the references to reflect new path. Generally I create a lib folder and place all the external libraries.
在 NuGet 中搜索并安装它。它将安装在包文件夹中,根据您的项目结构更改位置并重新添加引用以反映新路径。通常我会创建一个 lib 文件夹并放置所有外部库。
Folder structure of the project can be
项目的文件夹结构可以是
lib -> external libraries
src - > code
tools -> tools such as nant, etc
Microsoft.Web.Administrationis used to manage IIS programmatically and it is not available from add reference window. By adding reference from C:\Windows\System32\inetsrv would create reference issues. So, better to have it in lib folder internal to the project and check it in SVN.
Microsoft.Web.Administration用于以编程方式管理 IIS,它在添加引用窗口中不可用。通过添加来自 C:\Windows\System32\inetsrv 的引用会产生引用问题。因此,最好将它放在项目内部的 lib 文件夹中并在 SVN 中检查它。
回答by Lex Li
- If you project file, change the hint path to
<HintPath>%windir%\System32\inetsrv\Microsoft.Web.Administration.dll</HintPath>. - Check in. Then anyone who checks out should see the proper reference if IIS is installed. If IIS is not installed, there will be a reference error they need to resolve.
- 如果您投影文件,请将提示路径更改为
<HintPath>%windir%\System32\inetsrv\Microsoft.Web.Administration.dll</HintPath>. - 签入。然后签出的任何人都应该看到正确的参考,如果安装了 IIS。如果没有安装IIS,就会出现需要解决的引用错误。
To resolve, they either install IIS via Programs in Control Panel or via command line as @DzmitryLahoda pointed out.
为了解决这个问题,他们要么通过控制面板中的程序安装 IIS,要么通过@DzmitryLahoda 指出的命令行安装 IIS。
Again, any trick pointing you to the NuGet packages is improper,
同样,任何将您指向 NuGet 包的技巧都是不正确的,
回答by user3178007
C:\Windows\System32\inetsrv
C:\Windows\System32\inetsrv
or
或者
C:\Windows\winsxs\x86_microsoft.web.administration-nonmsil_31bf3856ad364e35_6.1.7600.16385_none_c8472944f22a9c81
C:\Windows\winsxs\x86_microsoft.web.administration-nonmsil_31bf3856ad364e35_6.1.7600.16385_none_c8472944f22a9c81
C:\Windows\WinSxS\x86_microsoft.web.administration-nonmsil_31bf3856ad364e35_6.3.9600.16384_none_5d1097a8ac709c4e
C:\Windows\WinSxS\x86_microsoft.web.administration-nonmsil_31bf3856ad364e35_6.3.9600.16384_none_5d1097a8ac709c4e
回答by Tom Winter
A bit of extra info, not a direct answer. IIS and IIS Express use two different versions of the same DLL, so your app might accidentally get the wrong one. See http://rdingwall.com/2013/09/22/microsoft-web-administration-confusion/
一些额外的信息,而不是直接的答案。IIS 和 IIS Express 使用同一 DLL 的两个不同版本,因此您的应用程序可能会意外获取错误的版本。见http://rdingwall.com/2013/09/22/microsoft-web-administration-confusion/
回答by Dzmitry Lahoda
Try to ensure these assemblies are installed via prebuild event:
尝试确保通过预构建事件安装这些程序集:
Dism /online /Enable-Feature /FeatureName:IIS-WebServerManagementTools /All
Dism /online /Enable-Feature /FeatureName:IIS-WebServerManagementTools /All
In installed whole IIS with all features this way, so used FeatureNameis guesstimate for assemblies. Try next to list all features for alternative:
在以这种方式安装了具有所有功能的整个 IIS 中,因此使用的FeatureName是对程序集的猜测。尝试列出替代的所有功能:
dism /online /Get-Features
dism /online /Get-Features
DISMis available for Windows earlier then 8:
To service inbox drivers in an offline image of Windows Vista with Service Pack 2 (SP2) or Windows Server 2008 with SP2, you must use the Windows 7 version of DISM or PkgMgr. The Windows 7 version of DISM is available in Windows 7 and Windows PE 3.0 operating systems, or can be installed with the Windows Automated Installation Kit (Windows AIK) or the Windows OEM Preinstallation Kit (Windows OPK) for Windows 7.
DISM适用于 Windows 8 之前的版本:
To service inbox drivers in an offline image of Windows Vista with Service Pack 2 (SP2) or Windows Server 2008 with SP2, you must use the Windows 7 version of DISM or PkgMgr. The Windows 7 version of DISM is available in Windows 7 and Windows PE 3.0 operating systems, or can be installed with the Windows Automated Installation Kit (Windows AIK) or the Windows OEM Preinstallation Kit (Windows OPK) for Windows 7.
Also can try for Windows 7 and below:
start /w pkgmgr /iu:IIS-WebServerManagementTools
也可以尝试 Windows 7 及以下:
start /w pkgmgr /iu:IIS-WebServerManagementTools
回答by Silverten
You could always just load the dll dynamically, and never directly reference it in your project. That way you avoid all of the "reference" issues, NuGet packages, and so on. However, you will still need to verify that the dll is available on the end target.
您总是可以动态加载 dll,而不要在您的项目中直接引用它。这样就可以避免所有“引用”问题、NuGet 包等。但是,您仍需要验证 dll 在最终目标上是否可用。
var windowsPath = Environment.GetFolderPath(Environment.SpecialFolder.Windows);
var DLL = Assembly.LoadFile($@"{windowsPath}\System32\inetsrv\Microsoft.Web.Administration.dll");
if (DLL != null)
{
// Get Server...
foreach (Type type in DLL.GetExportedTypes())
{
if (type.Name == "ServerManager")
{
dynamic server = Activator.CreateInstance(type);
dynamic applicationPools = server.ApplicationPools;
foreach (dynamic pool in applicationPools)
{
MessageBox.Show(pool.Name);
}
// Done -- Get Out...
break;
}
}
}
回答by thestar
The simplest way to do this is install NuGet in Visual Studio from this link. http://visualstudiogallery.msdn.microsoft.com/27077b70-9dad-4c64-adcf-c7cf6bc9970c
执行此操作的最简单方法是从此链接在 Visual Studio 中安装 NuGet。 http://visualstudiogallery.msdn.microsoft.com/27077b70-9dad-4c64-adcf-c7cf6bc9970c
Then, in Visual Studio, go to Tools->NuGet Package Manager-> Package Manager Console
然后,在 Visual Studio 中,转到 Tools->NuGet Package Manager-> Package Manager Console
Then, select Default Project to be the project that you want to install to.
然后,选择默认项目作为要安装到的项目。
Finally, Run Install-Package Microsoft.Web.Administrationcommand.
最后,运行Install-Package Microsoft.Web.Administration命令。

