簡體   English   中英

將DLL拖放到Windows Server 2008 .net 4.0中的GAC(“程序集”)

[英]Drag and drop a DLL to the GAC (“assembly”) in windows server 2008 .net 4.0

我試圖將一些代碼部署到客戶機,我不想安裝MS Windows SDK工具。 這意味着無法訪問“gacutil”。 我還沒有為我的代碼創建安裝程序。 看起來這些可能是.net 4.0中唯一的兩個選項。

在過去,我只需要開始,運行,鍵入“程序集”,然后拖放我的dll。

這不再可能嗎? 當我嘗試這樣做時,我沒有收到任何錯誤消息,但dll沒有出現在“assembly”文件夾中。 當我在我的開發機器上使用gacutil時它工作正常,但dll仍然沒有出現。

在.net 4.0中,Microsoft刪除了僅通過拖放將DLL添加到程序集的功能。

相反,您需要使用gacutil.exe,或創建安裝程序來執行此操作。 微軟實際上並不建議使用gacutil,但無論如何我都去了那條路線。

要在開發機器上使用gacutil,請訪問:
Start -> programs -> Microsoft Visual studio 2010 -> Visual Studio Tools -> Visual Studio Command Prompt (2010)

然后使用這些命令分別卸載和重新安裝。 注意我沒有在uninstall命令中包含.dll
gacutil /u myDLL
gacutil /i "C:\\Program Files\\Custom\\myDLL.dll"

要在非開發機器上使用Gacutil,您必須將可執行文件和配置文件從開發機器復制到生產機器。 看起來有幾個不同版本的Gacutil。 我找到了一個適合我的那個:
C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v7.0A\\Bin\\NETFX 4.0 Tools\\gacutil.exe C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v7.0A\\Bin\\NETFX 4.0 Tools\\gacutil.exe.config

將文件復制到相應的.net文件夾中;
C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319

然后使用這些命令分別卸載和重新安裝
"C:\\Users\\BHJeremy\\Desktop\\Installing to the Gac in .net 4.0\\gacutil.exe" /u "myDLL"

"C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\gacutil.exe" /i "C:\\Program Files\\Custom\\myDLL.dll"

在可能的情況下,我必須復制gacutil.exe,gacutil.exe.config以及gacutlrc.dll(來自1033目錄)

客戶端計算機上沒有gacutil實用程序,Window SDK許可證禁止將其重新分發給客戶。 當您的客戶不能,(並且實際上不應該)下載300MB Windows SDK作為應用程序安裝過程的一部分。

您(或您的安裝程序)可以使用官方支持的API在全局程序集緩存中注冊程序集。 Microsoft的Windows Installer技術知道如何為您調用此API。 您必須咨詢您的MSI安裝程序實用程序(例如WiX,InnoSetup),了解如何指示您希望程序集在全局程序集緩存中注冊的語法。

但MSI和gacutil沒有什么特別之處。 他們只需調用您可以自己調用的相同API。 有關如何通過代碼注冊程序集的文檔,請參閱:

KB317540:DOC:全局程序集緩存(GAC)API未在.NET Framework軟件開發工具包(SDK)文檔中記錄

var IAssemblyCache assemblyCache;
CreateAssemblyCache(ref assemblyCache, 0);


String manifestPath = "D:\Program Files\Contoso\Frobber\Grob.dll";

FUSION_INSTALL_REFERENCE refData;
refData.cbSize = SizeOf(refData); //The size of the structure in bytes
refData.dwFlags = 0; //Reserved, must be zero
refData.guidScheme = FUSION_REFCOUNT_FILEPATH_GUID; //The assembly is referenced by an application that is represented by a file in the file system. The szIdentifier field is the path to this file.
refData.szIdentifier = "D:\Program Files\Contoso\Frobber\SuperGrob.exe"; //A unique string that identifies the application that installed the assembly
refData.szNonCannonicalData = "Super cool grobber 9000"; //A string that is only understood by the entity that adds the reference. The GAC only stores this string

//Add a new assembly to the GAC. 
//The assembly must be persisted in the file system and is copied to the GAC.
assemblyCache.InstallAssembly(
      IASSEMBLYCACHE_INSTALL_FLAG_FORCE_REFRESH, //The files of an existing assembly are overwritten regardless of their version number
      manifestPath, //A string pointing to the dynamic-linked library (DLL) that contains the assembly manifest. Other assembly files must reside in the same directory as the DLL that contains the assembly manifest.
      refData);

刪除知識庫文章之前的更多文檔:

結構的字段定義如下:

  • cbSize - 結構的大小(以字節為單位)。
  • dwFlags - 保留,必須為零。
  • guidScheme - 添加引用的實體。
  • szIdentifier - 標識安裝程序集的應用程序的唯一字符串。
  • szNonCannonicalData - 只有添加引用的實體才能理解的字符串。 GAC僅存儲此字符串。

guidScheme字段的可能值可以是以下之一:

FUSION_REFCOUNT_MSI_GUID - 程序集由使用Windows Installer安裝的應用程序引用。 szIdentifier字段設置為MSIszNonCannonicalData設置為Windows Installer。 此方案只能由Windows Installer本身使用。 FUSION_REFCOUNT_UNINSTALL_SUBKEY_GUID - 程序集由“ 添加/刪除程序”中顯示的應用程序引用。 szIdentifier字段是用於向添加/刪除程序注冊應用程序的標記。 FUSION_REFCOUNT_FILEPATH_GUID - 程序集由文件系統中的文件表示的應用程序引用。 szIdentifier字段是此文件的路徑。 FUSION_REFCOUNT_OPAQUE_STRING_GUID - 程序集由僅由不透明字符串表示的應用程序引用。 szIdentifier是這個不透明的字符串。 刪除它時,GAC不會對不透明引用執行存在檢查。

如果您安裝了必要的.net框架。 Ex; .Net 4.0.Net 3.5 ,然后您可以從任何一台機器復制Gacutil.exe到新機器。

1)在新服務器中以管理員身份打開CMD。
2)遍歷到您復制Gacutil.exe的文件夾。 例如 - C:\\ program files。(在我的例子中)。
3)在cmd提示符下鍵入以下內容並安裝。

C:\\ Program Files \\ gacutil.exe /我dllname

安裝程序和gacutil的其他替代方法是GUI工具,如Gac ManagerGACAdmin 或者,如果你喜歡PowerShell,你可以使用我作為作者的PowerShell GAC

請記住,Fusion API是不受管理的。 它的當前參考是: 開發指南>非托管API參考> Fusion

但是,有一個托管方法可以將程序集添加到GAC:System.EnterpriseServices.Internal.Publish.GacInstall並且,如果需要注冊任何類型:System.EnterpriseServices.Internal.Publish.RegisterAssembly

發布類的引用位於: .NET Framework類庫> System.EnterpriseServices命名空間> System.EnterpriseServices.Internal

但是,這些方法旨在安裝Web服務應用程序(如ASP.NET或WCF)所需的組件。 因此,他們不會使用Fusion注冊組件; 因此,它們可以被其他應用程序卸載,或者使用gacutil,導致程序集停止工作。 因此,如果您在管理員管理GAC的Web服務器之外使用它們,請確保在SOFTWARE \\ Wow6432Node \\ Microsoft \\ Fusion \\ References(適用於64位操作系統)或SOFTWARE \\ Microsoft \\中添加對應用程序的引用Fusion \\ References(適用於32位操作系統),除非他們卸載您的應用程序,否則任何人都無法刪除您的支持程序集。

您也可以使用命令提示符將文件復制到GAC。 我使用以下批處理腳本來復制DLL並重新啟動IIS。

copy /b/v/y "PathToAssembly\MyAssembly.dll" "C:\Windows\assembly\" 
iisreset /noforce
pause

節省使用或安裝gacutil的需要

暫無
暫無

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

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