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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7787607/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:57:39  来源:igfitidea点击:

Drag and drop a DLL to the GAC ("assembly") in windows server 2008 .net 4.0

.net.net-4.0gac

提问by TizzyFoe

I've trying to deploy some code to a client machine, where I don't want to install MS windows SDK tools. This means don't have access to "gacutil". I also have not created an installer for my code. It seems that these might be the only two options in .net 4.0.

我试图将一些代码部署到我不想安装 MS Windows SDK 工具的客户端机器上。这意味着无法访问“gacutil”。我也没有为我的代码创建安装程序。似乎这可能是 .net 4.0 中仅有的两个选项。

In the past I would simply go to start, run, type "assembly", and drag and drop my dll in.

在过去,我只是简单地开始,运行,输入“程序集”,然后将我的 dll 拖放到其中。

Is this no longer possible? When I try to do this I get no error message, but the dll doesn't appear in the "assembly" folder. When I use gacutil on my dev machine it works properly, but the dll still doesn't appear.

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

回答by TizzyFoe

In .net 4.0 Microsoft removed the ability to add DLLs to the Assembly simply by dragging and dropping.

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

Instead you need to use gacutil.exe, or create an installer to do it. Microsoft actually doesn't recommend using gacutil, but I went that route anyway.

相反,您需要使用 gacutil.exe,或创建一个安装程序来执行此操作。微软实际上不推荐使用 gacutil,但我还是走那条路。

To use gacutil on a development machine go to:
Start -> programs -> Microsoft Visual studio 2010 -> Visual Studio Tools -> Visual Studio Command Prompt (2010)

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

Then use these commands to uninstall and Reinstall respectively. Note I did NOT include .dllin the uninstall command.
gacutil /u myDLL
gacutil /i "C:\Program Files\Custom\myDLL.dll"

然后使用这些命令分别卸载和重新安装。注意我没有包含.dll在卸载命令中。
gacutil /u myDLL
gacutil /i "C:\Program Files\Custom\myDLL.dll"

To use Gacutil on a non-development machine you will have to copy the executable and config file from your dev machine to the production machine. It looks like there are a few different versions of Gacutil. The one that worked for me, I found here:
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\gacutil.exeC:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\gacutil.exe.config

要在非开发机器上使用 Gacutil,您必须将可执行文件和配置文件从开发机器复制到生产机器。看起来 Gacutil 有几个不同版本。对我有用的那个,我在这里找到了:
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\gacutil.exeC:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\gacutil.exe.config

Copy the files here or to the appropriate .net folder;
C:\Windows\Microsoft.NET\Framework\v4.0.30319

将文件复制到此处或到相应的 .net 文件夹;
C:\Windows\Microsoft.NET\Framework\v4.0.30319

Then use these commands to uninstall and reinstall respectively
"C:\Users\BHJeremy\Desktop\Installing to the Gac in .net 4.0\gacutil.exe" /u "myDLL"

然后使用这些命令分别卸载和重新安装
"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"

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

回答by Roberto Montemor

In may case, I nedded to copy the gacutil.exe, gacutil.exe.config AND ALSO the gacutlrc.dll (from the 1033 directory)

在可能的情况下,我需要复制 gacutil.exe、gacutil.exe.config 和 gacutlrc.dll(来自 1033 目录)

回答by Ian Boyd

The gacutilutility is not available on client machines, and the Window SDK license forbids redistributing it to your customers. When your customer can not, will not, (and really should not) download the 300MB Windows SDK as part of your application's install process.

gacutil实用程序在客户端计算机上不可用,并且 Window SDK 许可证禁止将其重新分发给您的客户。当您的客户不能、不会(并且真的不应该)下载 300MB Windows SDK 作为应用程序安装过程的一部分时。

There is an officially supported API you (or your installer) can use to register an assembly in the global assembly cache. Microsoft's Windows Installer technology knows how to call this API for you. You would have to consult your MSI installer utility (e.g. WiX, InnoSetup) for their own syntax of how to indicate you want an assembly to be registered in the Global Assembly Cache.

您(或您的安装程序)可以使用官方支持的 API 在全局程序集缓存中注册程序集。Microsoft 的 Windows Installer 技术知道如何为您调用此 API。您必须咨询您的 MSI 安装程序实用程序(例如 WiX、InnoSetup),了解他们自己的语法,以了解如何指示您希望将程序集注册到全局程序集缓存中。

But MSI, and gacutil, are doing nothing special. They simply call the same API you can call yourself. For documentation on how to register an assembly through code, see:

但是 MSI 和 gacutil 没有做任何特别的事情。它们只是调用您可以调用的相同 API。有关如何通过代码注册程序集的文档,请参阅:

KB317540: DOC: Global Assembly Cache (GAC) APIs Are Not Documented in the .NET Framework Software Development Kit (SDK) Documentation

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);

More documentation before the KB article is deleted:

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

The fields of the structure are defined as follows:

  • cbSize- The size of the structure in bytes.
  • dwFlags- Reserved, must be zero.
  • guidScheme- The entity that adds the reference.
  • szIdentifier- A unique string that identifies the application that installed the assembly.
  • szNonCannonicalData- A string that is only understood by the entity that adds the reference. The GAC only stores this string.

Possible values for the guidScheme field can be one of the following:

FUSION_REFCOUNT_MSI_GUID- The assembly is referenced by an application that has been installed by using Windows Installer. The szIdentifierfield is set to MSI, and szNonCannonicalDatais set to Windows Installer. This scheme must only be used by Windows Installer itself. FUSION_REFCOUNT_UNINSTALL_SUBKEY_GUID- The assembly is referenced by an application that appears in Add/Remove Programs. The szIdentifierfield is the token that is used to register the application with Add/Remove programs. FUSION_REFCOUNT_FILEPATH_GUID- The assembly is referenced by an application that is represented by a file in the file system. The szIdentifierfield is the path to this file. FUSION_REFCOUNT_OPAQUE_STRING_GUID - The assembly is referenced by an application that is only represented by an opaque string. The szIdentifier is this opaque string. The GAC does not perform existence checking for opaque references when you remove this.

结构体的字段定义如下:

  • 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 不会对不透明引用执行存在检查。

回答by Ganesh Shanmugam

if you have neccessary .netframework installed. Ex ; .Net 4.0or .Net 3.5, then you can just copy Gacutil.exefrom any of the machine and to the new machine.

如果您安装了必要的.net框架。前任 ; .Net 4.0或者.Net 3.5,然后您可以Gacutil.exe从任何一台机器复制到新机器。

1) Open CMD as adminstrator in new server.
2) Traverse to the folder where you copied the Gacutil.exe. For eg - C:\program files.(in my case).
3) Type the below in the cmd prompt and install.

1) 在新服务器中以管理员身份打开 CMD。
2) 遍历到您复制 Gacutil.exe 的文件夹。例如 - C:\program 文件。(在我的情况下)。
3)在cmd提示符下输入以下内容并安装。

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

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

回答by Lars Truijens

Other alternatives to an installer and gacutil are GUI tools like Gac Manageror GACAdmin. Or if you like PowerShell you could use PowerShell GACfrom which I am the author.

安装程序和 gacutil 的其他替代方案是 GUI 工具,如Gac ManagerGACAdmin。或者,如果您喜欢 PowerShell,您可以使用我是作者的PowerShell GAC

回答by Steve Wood

Keep in mind that the Fusion API is unmanaged. The current reference for it is here: Development Guide > Unmanaged API Reference > Fusion

请记住,Fusion API 是非托管的。它的当前参考在这里:开发指南 > 非托管 API 参考 > Fusion

However, there is a managed method to add an assembly to GAC: System.EnterpriseServices.Internal.Publish.GacInstall And, if you need to register any Types: System.EnterpriseServices.Internal.Publish.RegisterAssembly

但是,有一种向 GAC 添加程序集的托管方法: System.EnterpriseServices.Internal.Publish.GacInstall 并且,如果您需要注册任何类型: System.EnterpriseServices.Internal.Publish.RegisterAssembly

The reference for the publish class is here: .NET Framework Class Library > System.EnterpriseServices Namespaces > System.EnterpriseServices.Internal

发布类的参考在这里:.NET Framework Class Library > System.EnterpriseServices Namespaces > System.EnterpriseServices.Internal

However, these methods were designed for installing components that are required by a web service application such as ASP.NET or WCF. As a result they don't register the assemblies with Fusion; thus, they can be uninstalled by other applications, or using gacutil and cause your assembly to stop working. So, if you use them outside of a web server where an administrator is managing the GAC then be sure to add a reference to your application in SOFTWARE\Wow6432Node\Microsoft\Fusion\References (for 64-bit OS) or SOFTWARE\Microsoft\Fusion\References (for 32-bit OS) so that nobody can remove your support assemblies unless they uninstall your application.

但是,这些方法是为安装 Web 服务应用程序(如 ASP.NET 或 WCF)所需的组件而设计的。因此,他们不会向 Fusion 注册程序集;因此,它们可以被其他应用程序卸载,或者使用 gacutil 并导致您的程序集停止工作。因此,如果您在管理员管理 GAC 的 Web 服务器之外使用它们,请确保在 SOFTWARE\Wow6432Node\Microsoft\Fusion\References(对于 64 位操作系统)或 SOFTWARE\Microsoft\ 中添加对您的应用程序的引用Fusion\References(适用于 32 位操作系统)以便没有人可以删除您的支持程序集,除非他们卸载您的应用程序。

回答by Paul Grimshaw

You can also just copy the file to GAC using a command prompt. I use the following batch script to copy the DLL and restart IIS.

您也可以使用命令提示符将文件复制到 GAC。我使用以下批处理脚本复制 DLL 并重新启动 IIS。

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

Saves the need to use or install gacutil

无需使用或安装 gacutil