将DLL拖放到Windows Server 2008 .net 4.0中的GAC("程序集")

Tiz*_*Foe 30 .net gac .net-4.0

我试图将一些代码部署到客户机,我不想安装MS Windows SDK工具.这意味着无法访问"gacutil".我还没有为我的代码创建安装程序.看起来这些可能是.net 4.0中唯一的两个选项.

在过去,我只需要开始,运行,键入"程序集",然后拖放我的dll.

这不再可能吗?当我尝试这样做时,我没有收到任何错误消息,但dll没有出现在"assembly"文件夹中.当我在我的开发机器上使用gacutil时它工作正常,但dll仍然没有出现.

Tiz*_*Foe 53

在.net 4.0中,Microsoft删除了仅通过拖放将DLL添加到程序集的功能.

相反,您需要使用gacutil.exe,或创建安装程序来执行此操作.微软实际上并不建议使用gacutil,但无论如何我都去了那条路线.

要在开发机器上使用gacutil,请访问:
Start -> programs -> Microsoft Visual studio 2010 -> Visual Studio Tools -> Visual Studio Command Prompt (2010)

然后使用这些命令分别卸载和重新安装.注意我没有包含.dll在uninstall命令中.
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"

  • 为什么那些混蛋!(夸张地开玩笑地说)他们为什么要删除这种能力?这是添加GAC的完全合法的方式.*悲伤的脸* (7认同)
  • 另请注意,GAC针对不同的CLR进行了拆分..NET 4位置是`%windir%\ Microsoft.NET\assembly \`. (2认同)
  • 您是否可以编辑以包含复制gacutlrc.dll,正如Roberto在下面建议的那样? (2认同)

小智 21

在可能的情况下,我必须复制gacutil.exe,gacutil.exe.config以及gacutlrc.dll(来自1033目录)

  • 谢谢,是的,这也是为我做的.我将gacutlrc.dll复制到服务器上与gacutil.exe相同的文件夹中,这次成功注册了dll. (2认同)

Ian*_*oyd 5

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);
Run Code Online (Sandbox Code Playgroud)

KB文章删除前的更多文档:

结构体的字段定义如下:

  • cbSize - 结构的大小(以字节为单位)。
  • dwFlags - 保留,必须为零。
  • guidScheme - 添加引用的实体。
  • szIdentifier - 一个唯一的字符串,用于标识安装程序集的应用程序。
  • szNonCannonicalData - 只有添加引用的实体才能理解的字符串。GAC 仅存储此字符串。

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

FUSION_REFCOUNT_MSI_GUID- 程序集被使用Windows Installer安装的应用程序引用。该szIdentifier字段设置为MSI,并szNonCannonicalData设置为Windows安装程序。此方案只能由 Windows Installer 本身使用。 FUSION_REFCOUNT_UNINSTALL_SUBKEY_GUID- 程序集被添加/删除程序中出现的应用程序引用。该szIdentifier字段是用于注册与应用程序令牌添加/删除程序FUSION_REFCOUNT_FILEPATH_GUID- 程序集由文件系统中的文件表示的应用程序引用。该szIdentifier字段是此文件的路径。FUSION_REFCOUNT_OPAQUE_STRING_GUID - 程序集由仅由不透明字符串表示的应用程序引用。szIdentifier 是这个不透明的字符串。当您删除它时,GAC 不会对不透明引用执行存在检查。