簡體   English   中英

如何引用 Microsoft.Web.Administration?

[英]How to reference Microsoft.Web.Administration?

Microsoft.Web.Administration 程序集位於我機器上的C:\Windows\System32\inetsrv中。 我相信它是作為 IIS 的一部分安裝的。 該程序集也在 GAC 中。

鑒於我想將項目提交給 SVN 以供其他人簽出,我應該如何從我的項目中引用這個程序集。 Microsoft.Web.Administration未出現在 Visual Studio 的“添加引用”列表中。 我可以添加對C:\Windows\System32\inetsrv\Microsoft.Web.Administration的引用,但這似乎是個壞主意,因為其他開發人員可能將它安裝在不同的路徑或驅動器上。

或者我可以將它復制到項目文件夾中,但是我必須將二進制文件提交給 SVN。

以下步驟對我有用:

  1. 轉到C:\Windows\System32\inetsrv並檢查文件Microsoft.Web.Administration.dll是否存在。 如果您在C:\Windows\System32\inetsrv中缺少Microsoft.Web.Administration.dll ,請在 Windows 功能中啟用 IIS 管理控制台:

將管理服務添加到 Windows 功能

  1. 在您的 Visual Studio 項目中,添加對Microsoft.Web.Administration.dll的引用: 在此處輸入圖像描述
  2. 現在您的 .proj 文件具有以下條目: <HintPath>C:\Windows\System32\inetsrv\Microsoft.Web.Administration.dll</HintPath> 您可以將此路徑調整為例如<HintPath>%windir%\System32\inetsrv\Microsoft.Web.Administration.dll</HintPath>以使其更加健壯。

當您將應用程序移至其他系統時,該應用程序還會引用目標系統上的Microsoft.Web.Administration.dll 由於 Windows 系統之間的Microsoft.Web.Administration.dll可能不同,因此不建議應用程序提供此程序集文件的副本。 副本可能與目標系統上的 IIS 不兼容。

也不建議在同一系統上引用程序集的副本,因為Microsoft.Web.Administrator.dll和 IIS 可能會因 Windows 更新而改變! 那么您引用的Microsoft.Web.Administrator.dll副本與更新后的 IIS 不兼容。

當目標系統上缺少 Microsoft.Web.Administrator.dll 時,請按照上述步驟 1 中的說明激活管理控制台。 或者,您可以使用 cmd(以管理員身份打開)並通過DISM.exe通過以下命令激活 Windows 功能管理控制台:

C:\Windows\System32\Dism.exe /enable-feature /online /featurename:IIS-ManagementConsole

當您想通過設置分發應用程序時,此命令可能很有用。

您可以手動修改項目文件。 添加/更改如下引用將在 GAC 中找到程序集,無論其位置如何:

<Reference Include="Microsoft.Web.Administration, Version=7.9.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
    <SpecificVersion>False</SpecificVersion>
</Reference>

當然,如果僅安裝了“IIS 管理控制台”功能。 您可以簡單地在代碼中留下提示:

#warning Windows feature "IIS management console" must be installed locally
  1. 如果您項目文件,請將提示路徑更改為<HintPath>$(windir)\System32\inetsrv\Microsoft.Web.Administration.dll</HintPath>
  2. 簽入。然后,如果安裝了 IIS,任何簽出的人都應該看到正確的參考。 如果未安裝 IIS,則會出現他們需要解決的參考錯誤。

為了解決這個問題,他們要么通過控制面板中的程序安裝 IIS,要么通過@DzmitryLahoda 指出的命令行安裝 IIS。

同樣,任何指向 NuGet 包的技巧都是不恰當的,

https://halfblood.pro/whats-microsoft-web-administration-and-the-horrible-facts-you-should-know-b82f2c974da6

在 NuGet 中搜索並安裝它。 它將安裝在包文件夾中,根據您的項目結構更改位置並重新添加引用以反映新路徑。 通常我會創建一個 lib 文件夾並放置所有外部庫。

項目的文件夾結構可以是

lib -> external libraries
src - > code
tools -> tools such as nant, etc

Microsoft.Web.Administration用於以編程方式管理 IIS,它在添加參考窗口中不可用。 通過從 C:\Windows\System32\inetsrv 添加引用會產生引用問題。 因此,最好將它放在項目內部的 lib 文件夾中並在 SVN 中檢查它。

C:\Windows\System32\inetsrv

或者

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

使用Nuget

Microsoft.Web.Administration

在此處輸入圖像描述

嘗試確保通過 prebuild 事件安裝這些程序集:

Dism /online /Enable-Feature /FeatureName:IIS-WebServerManagementTools /All

以這種方式安裝了具有所有功能的整個 IIS,因此使用的FeatureName是對程序集的猜測。 嘗試下一步列出所有替代功能:

dism /online /Get-Features

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. 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.

也可以嘗試 Windows 7 及以下版本: start /w pkgmgr /iu:IIS-WebServerManagementTools

一些額外的信息,而不是直接的答案。 IIS 和 IIS Express 使用同一個 DLL 的兩個不同版本,因此您的應用程序可能會意外獲取錯誤的版本。 請參閱http://rdingwall.com/2013/09/22/microsoft-web-administration-confusion/

您總是可以動態加載 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;
        }
    }
}

執行此操作的最簡單方法是從此鏈接在 Visual Studio 中安裝 NuGet。 http://visualstudiogallery.msdn.microsoft.com/27077b70-9dad-4c64-adcf-c7cf6bc9970c

然后,在 Visual Studio 中,轉到工具-> NuGet 包管理器-> 包管理器控制台

然后,選擇默認項目作為您要安裝到的項目。

最后,運行Install-Package Microsoft.Web.Administration命令。

將名為“引用程序集”的新項目添加到您的解決方案中,並將 DLL 添加到該項目中 - 這樣 DLL 將位於源代碼管理中。 然后,您可以使用添加引用中的“瀏覽”選項來引用該項目。

您的同事會很高興,因為他們不會錯過源代碼控制中的這個 DLL。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM