过去,我会简单地转到“开始”,运行,输入“assembly”,然后拖放我的dll文件进去。
这还有可能吗?当我尝试这样做时,没有错误信息,但是dll文件未出现在“assembly”文件夹中。当我在开发机上使用gacutil时,它可以正常工作,但dll文件仍未出现。
在 .net 4.0 中,微软取消了通过拖放方式将DLL添加到程序集的功能。
相反,您需要使用gacutil.exe或创建安装程序来执行此操作。 实际上,微软不建议使用gacutil,但我还是选择了这种方法。
要在开发计算机上使用gacutil,请转到:
开始 -> 程序 -> Microsoft Visual studio 2010 -> Visual Studio Tools -> Visual Studio Command Prompt (2010)
然后使用以下命令进行卸载和重新安装。请注意,卸载命令中未包括 .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"
%windir%\Microsoft.NET\assembly\
。 - Chris在我的情况下,我需要复制gacutil.exe,gacutil.exe.config以及gacutlrc.dll(从1033目录中)
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\1033
。 - chridamgacutil
实用程序在客户机上不可用,且Window SDK许可证禁止将其分发给您的客户。当您的客户无法、不会(而且确实不应该)在安装过程中下载300MB的Windows SDK时。
有一种官方支持的API可以用于在全局程序集缓存中注册程序集,您(或安装程序)可以使用此API。Microsoft的Windows Installer技术知道如何为您调用此API。您必须参考您的MSI安装程序工具(例如WiX,InnoSetup)的语法,以指示您希望在全局程序集缓存中注册程序集。
但是MSI和gacutil并没有做什么特别的事情。他们只是调用您自己可以调用的相同API。有关通过代码注册程序集的文档,请参见:
KB317540:DOC:未在.NET Framework软件开发工具包(SDK)文档中记录全局程序集缓存(GAC)API
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);
除了安装程序和gacutil之外,还有一些图形界面工具可供选择,比如Gac Manager或GACAdmin。如果你喜欢PowerShell,你可以使用我编写的PowerShell GAC。
您也可以使用命令提示符将文件复制到GAC。我使用以下批处理脚本来复制DLL并重新启动IIS。
copy /b/v/y "PathToAssembly\MyAssembly.dll" "C:\Windows\assembly\"
iisreset /noforce
pause
节省了使用或安装gacutil的需求