windows 向 GAC 注册/安装程序集的“正确”方式是什么?

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

What's the 'correct' way of registering/installing an Assembly to the GAC?

.netwindowsinstallergac

提问by Robert P

There seems to be lots of different ways to register assemblies with the GAC, as in, they 'work'. However, what's the "proper" way of doing it?

似乎有很多不同的方法可以向 GAC 注册程序集,因为它们“有效”。但是,这样做的“正确”方式是什么?

In response to Lou Franco (and gacutil):

回应 Lou Franco(和 gacutil):

I'm using Gacutil for development, but it seems to me to be not the proper way to install it, since gacutil isn't included in the basic .NET utilities past .NET 1.1 - it's only a developer tool.

我正在使用 Gacutil 进行开发,但在我看来这不是安装它的正确方法,因为 gacutil 不包含在 .NET 1.1 之后的基本 .NET 实用程序中 - 它只是一个开发人员工具。

Additional: Gacutil (as seen in responses below) is not redistributable, and therefore should not be used in any app that you intend to give to people who are not developers. AKA, customers. See This blog post (and comments) by Aaron Stebner.

附加: Gacutil(如下面的回复所示)不可再分发,因此不应在您打算提供给非开发人员的任何应用程序中使用。又名,客户。请参阅Aaron Stebner 的这篇博文(和评论)

In response to using WIX:

响应使用 WIX:

WIX might be great and all, but how does it work under the hood? What details makes the way WIX installs the assembly the right way to install it? How does it look it up? Is it a system/.NET call? Is there some call in a dll buried somewhere in System32 that needs to be made?

WIX 可能很棒,但它是如何在引擎盖下工作的?哪些细节使 WIX 安装程序集的方式成为正确的安装方式?它怎么看?它是系统/.NET 调用吗?是否有需要在 System32 中某处埋藏的 dll 中进行调用?

(Edit: it looks like WIX uses MSI under the hood. See my comments in the accepted answer.)

(编辑:看起来 WIX 在幕后使用 MSI。请参阅我在已接受答案中的评论。)

Final edit: It looks like the correct way to install an assembly to the GAC is using windows installer, and nothing else. I'm going to give Wix a try. Thanks all!

最终编辑:看起来将程序集安装到 GAC 的正确方法是使用 Windows 安装程序,仅此而已。我要试试 Wix。谢谢大家!

回答by CheGueVerra

With Wix I would do something like this:

使用 Wix 我会做这样的事情:

<DirectoryRef Id="MyDirectory" >
    <Component Id="MyComponent" Guid="PUT-GUID-HERE" DiskId="1">
        <File Id="MyAssembly" Name="MyAssembly.dll" Assembly=".net" KeyPath="yes" Source="MyAssembly.dll" />
    </Component>
</DirectoryRef>

When you use the attribute Assembly=".net" for a file in WiX, it will create entries in the MsiAssembly and MsiAssemblyName table for this component and mark it as a GAC component.

当您在 WiX 中为文件使用属性 Assembly=".net" 时,它将在 MsiAssembly 和 MsiAssemblyName 表中为此组件创建条目并将其标记为 GAC 组件。

回答by Robert P

http://blogs.msdn.com/astebner/archive/2006/11/04/why-to-not-use-gacutil-exe-in-an-application-setup.aspx

http://blogs.msdn.com/astebner/archive/2006/11/04/why-to-not-use-gacutil-exe-in-an-application-setup.aspx

It looks like the gacutil should be avoided; it's not a redistributable app. Instead, the 'proper' way of installing them seems to be using MSI, one way being WIX, as posted by CheGueVerra or another script.

看起来应该避免使用 gacutil;它不是一个可再发行的应用程序。相反,安装它们的“正确”方式似乎是使用 MSI,一种方式是 WIX,正如 CheGueVerra 或其他脚本发布的那样。

回答by Robert P

Use System.EnterpriseServices.Internal.Publish's GacInstallmethod.

使用System.EnterpriseServices.Internal.PublishGacInstall方法。

Advantages: Seems to be an internal tool. Probably does all the right stuff.

优点:似乎是一个内部工具。可能会做所有正确的事情。

Disadvantages: As part of an installer, you'd still need to make and run an app that calls this (unless the installer you make is a custom app that does it anyway).

缺点:作为安装程序的一部分,您仍然需要制作并运行一个调用它的应用程序(除非您制作的安装程序是一个无论如何都会执行的自定义应用程序)。

回答by Robert P

use gacutil.

使用 gacutil。

Advantages: seems to always work. Disadvantages:

优点:似乎总是有效。缺点:

  • must package an additional executable with your installer.
  • As a development utility, seems to have additional side effects (like forcing the install no matter what).
  • Should NOT be included in any redistributable given to customers.
  • 必须使用安装程序打包附加的可执行文件。
  • 作为开发实用程序,似乎有额外的副作用(例如无论如何都要强制安装)。
  • 不应包含在提供给客户的任何可再发行产品中。

回答by Lou Franco

Doesn't your installer maker have a way of installing an assembly into the GAC? On your own, I'd say GACUTIL:

您的安装程序制造商没有办法将程序集安装到 GAC 中吗?就你个人而言,我会说 GACUTIL:

http://msdn.microsoft.com/en-us/library/ex0ss12c(VS.80).aspx

http://msdn.microsoft.com/en-us/library/ex0ss12c(VS.80).aspx

回答by coder1

If you don't want to handle the gacutil stuff yourself, you can always create a setup project in visual studio.

如果你不想自己处理 gacutil 的东西,你总是可以在 Visual Studio 中创建一个安装项目。

But I'd stick with gacutil myself.

但我自己会坚持使用 gacutil。

回答by Mathieu Garstecki

The best way is to use gacutil -i Library.dll.

最好的方法是使用gacutil -i Library.dll.

The only problem with gacutil is that it is not in the default PATH of the system. It is however in a fixed location relative to the windows directory, for a given .Net Framework version. So you can use the following command line to execute it from anywhere:

gacutil 的唯一问题是它不在系统的默认 PATH 中。但是,对于给定的 .Net Framework 版本,它位于相对于 windows 目录的固定位置。因此,您可以使用以下命令行从任何地方执行它:

%SystemRoot%\Microsoft.Net\Framework\v1.1.4322\gacutil -i

PS: just copying your assembly into c:\windows\assembly won't work. Explorer only shows a simplified view of the folder, which contains in fact lots of different folders for different kinds of assemblies. Doing a copy in it from an installer won't trigger all the operations done by explorer on a drag-and-drop. (written here because I don't have enough reputation yet to comment on other posts).

PS:只是将你的程序集复制到 c:\windows\assembly 是行不通的。资源管理器仅显示文件夹的简化视图,其中实际上包含许多用于不同类型程序集的不同文件夹。从安装程序中复制它不会触发资源管理器在拖放时完成的所有操作。(写在这里是因为我还没有足够的声誉来评论其他帖子)。

回答by Robert P

copy directly to %WINDIR%\Assembly.

直接复制到 %WINDIR%\Assembly。

Advantage: Straightforward.

优点:直截了当。

Disadvantage: AFAIK, %WINDIR%\Assembly just happensto be where it is right now, and it's location is subject to change. This would make it break in future versions of windows or if that folder's behavior chaneges. This probably isn't the right way.

缺点:AFAIK,%WINDIR%\Assembly恰好在它现在的位置,它的位置可能会发生变化。如果该文件夹的行为发生变化,这将使其在未来版本的 Windows 中中断。这可能不是正确的方法。

Extreme Disadvantage: As said by madmath:

极端劣势:正如 madmath 所说:

just copying your assembly into c:\windows\assembly won't work. Explorer only shows a simplified view of the folder, which contains in fact lots of different folders for different kinds of assemblies. Doing a copy in it from an installer won't trigger all the operations done by explorer on a drag-and-drop. (written here because I don't have enough reputation yet to comment on other posts).
只是将您的程序集复制到 c:\windows\assembly 是行不通的。资源管理器仅显示文件夹的简化视图,其中实际上包含许多用于不同类型程序集的不同文件夹。从安装程序中复制它不会触发资源管理器在拖放时完成的所有操作。(写在这里是因为我还没有足够的声誉来评论其他帖子)。