使用 GAC 的优点和缺点是什么?

发布于 2024-07-04 11:10:44 字数 37 浏览 5 评论 0原文

除此之外,是否存在必须使用全局程序集缓存或不能使用它的情况?

And on top of that, are there cases where one has to use the global assembly cache or where one cannot use it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

剪不断理还乱 2024-07-11 11:10:44

优点:

  • 只有一个地方可以更新程序集
  • 您使用的硬盘空间稍微少一点

缺点:

  • 如果您只需要更新一个网站,则不能。 您可能会因网络服务器中的其他网站损坏而结束

建议:将 GAC 留给 MS 和朋友。 现在千兆字节很便宜。

Advantage:

  • Only one place to update your assemblys
  • You use a little less hard drive space

Disadvantage:

  • If you need to update only one website, you can't. You may end with the other websites in the webserver broken

Recommendation: Leave the GAC to MS and friends. The gigabyte is very cheap now.

葵雨 2024-07-11 11:10:44
  • 从 GAC 加载程序集意味着更少的开销和安全性,您的应用程序将始终加载正确版本的 .NET 库。
  • 您不应该生成 GAC 外部的程序集,因为几乎不会有任何性能增益,在许多情况下甚至会损失性能。
  • 您已经在使用 GAC,因为所有标准 .NET 程序集实际上都在 GAC 中并生成(在安装期间)。
  • 对您自己的库使用 GAC 会增加部署的复杂性,我会不惜一切代价尝试避免它。
  • 如果您想将某些内容放入 GAC 中,您的用户需要在安装过程中以管理员身份登录,这对于许多类型的应用程序来说都是一个问题。

总而言之,从简单开始,如果您稍后将程序集放入 GAC 和 NGEN 中看到性能大幅提升,那就继续吧,否则就不用费心了。 GAC更适合那些期望库在更多应用程序之间共享的框架,在99%的情况下,你不需要它。

  • Loading assemblies from GAC mean less overhead and security that your application will always load correct version of .NET library
  • You shouldn't ngen assemblies that are outside of GAC, because there will be almost no performance gain, in many cases even loss in performance.
  • You're already using GAC, because all standard .NET assemblies are actually in GAC and ngened (during installation).
  • Using GAC for your own libraries adds complexity into deployment, I would try to avoid it at all costs.
  • Your users need to be logged as administrators during installation if you want to put something into GAC, quite a problem for many types of applications.

So to sum it all, start simple and if you later see major performance gains if you put your assemblies into GAC and NGEN them, go for it, otherwise don't bother. GAC is more suitable for frameworks where there is expectation for library to be shared among more applications, in 99% of cases, you don't need it.

夜唯美灬不弃 2024-07-11 11:10:44

在我的一生中,我可能有一个应用程序,我必须将程序集放入 GAC 中,仅仅是因为这些程序集是许多应用程序将使用它的框架的一部分,并且将它们放入 GAC 中似乎是正确的。

In all my life, I have had maybe one application where I had to put an assembly in the GAC, simply because these assemblies were part of a framework that a number of applications would use it, and it seemed right to put them into the GAC.

夜灵血窟げ 2024-07-11 11:10:44

我认为使用 GAC 的最大优点之一是您可以注册同一程序集的多个版本并可供您的应用程序使用。 就我个人而言,我不喜欢它如何限制从一台机器到另一台机器的移动(我不喜欢不得不说,检查新 VPC 上的源代码并执行一系列步骤来让它运行,因为我必须在政府咨询委员会 (GAC)

I think one of the biggest advantages of using the GAC is that you can have multiple versions of the same assembly registered and available to your applications. Personally, i don't like how it restricts movement from machine to machine (i don't like having to say, check out source on a new VPC and go through a bunch of steps to get it running because I have to register stuff in the GAC)

回忆躺在深渊里 2024-07-11 11:10:44

如果您要交付由多个程序集组成的可重用库,但只有少数程序集形成外观,并且该程序包已安装到开发人员的 PC 上,则可以考虑将程序集安装到 GAC 中。

想象一下,您运送了 6 个程序集,而这 6 个程序集中只有一个包含外观 - 即其他 5 个程序集仅由外观本身使用。 您运送:

  • MyProduct.Facade.dll - 这是唯一供开发人员使用的组件
  • 使用
  • MyProduct.Core.dll - 由 MyProduct.Facade.dll 使用,但不供开发人员MyProduct 。 Component1.dll - 相同的
  • MyProduct.Component2.dll - 相同的
  • ThirdParty.Lib1.dll - MyProduct.Component1.dll 使用的第三方库
  • ThirdParty.Lib2.dll - 相同的
  • 等。

使用您的项目的开发人员只想引用MyProduct.Facade.dll 在他们自己的项目中。 但是当他们的项目运行时,它必须能够递归地加载它引用的所有程序集。 如何实现这一目标? 一般来说,它们必须在 Bin 文件夹中或 GAC 中可用:

  • 您可以要求开发人员找到您的安装文件夹并添加对您放置在那里的所有 N 程序集的引用。 这将确保它们被复制到 Bin 文件夹中以便在运行时可用。
  • 您可以安装已包含这 6 个引用的 VS.NET 项目模板。 有点复杂,因为您应该在安装之前将程序集的实际路径注入到此模板中。 这只能由安装程序完成,因为该路径取决于安装路径。
  • 您可以要求开发人员在 .csproj / .vbproj 文件中创建一个特殊的构建后步骤,将必要的依赖项复制到 Bin 文件夹。 同样的缺点。
  • 最后,您可以将所有程序集安装到 GAC 中。 在这种情况下,开发人员必须从其项目中仅添加对 MyProduct.Facade.dll 的引用。 无论如何,其他一切都将在运行时可用。

注意:最后一个选项并不要求您在将项目传送到生产 PC 时执行相同的操作。 您可以将所有程序集发送到 Bin 文件夹中,也可以将它们安装到 GAC 中 - 一切都取决于您的意愿。

因此,所描述的解决方案显示了在开发过程中将第三方程序集放入 GAC 中的优势。 与生产无关。

您可能会发现,安装到 GAC 主要是为了解决所需程序集(依赖项)的位置问题。 如果程序集安装到 GAC 中,您可能会认为它存在于任何应用程序“附近”。 这就像将 .exe 的路径添加到 PATH 变量中,但是是以“托管方式”。 - 当然,这是相当简化的描述;)

If you're shipping a reusable library consisting of multiple assemblies, but only few of them form a facade, you can consider installing the assemblies into GAC, if the package is installed to developer's PCs.

Imagine, you ship 6 assemblies, and only one of these 6 assemblies contains a facade - i.e. other 5 are used only by the facade itself. You ship:

  • MyProduct.Facade.dll - that's the only component intended to be used by developers
  • MyProduct.Core.dll - used by MyProduct.Facade.dll, but not intended to be used by developers
  • MyProduct.Component1.dll - the same
  • MyProduct.Component2.dll - the same
  • ThirdParty.Lib1.dll - third-party library used by MyProduct.Component1.dll
  • ThirdParty.Lib2.dll - the same
  • etc.

Developers using your project would like to reference just MyProduct.Facade.dll in their own projects. But when their project runs, it must be able to load all the assemblies it references - recursively. How this can be achieved? In general, they must be available either in Bin folder, on in GAC:

  • You may ask the developers to locate your installation folder and add references to all N assemblies you put there. This will ensure they'll be copied into Bin folder to be available in runtime.
  • You may install VS.NET project template already containing these 6 references. A bit complex, since you should inject the actual path to your assemblies into this template before its installation. This can be done only by installer, since this path depends on installation path.
  • You may ask developers to create a special post-build step in .csproj / .vbproj file copying the necessary dependencies to Bin folder. The same disadvantages.
  • Finally, you may install all your assemblies into GAC. In this case developers must add the reference just to MyProduct.Facade.dll from their project. Everything else will be available in runtime anyway.

Note: last option doesn't make you to do the same while shipping the project to production PCs. You can either ship all the assemblies within Bin folder, or install them into GAC - all depends all your wish.

So the solution described shows the advantage of putting third-party assemblies into GAC during the development. It doesn't related to production.

As you may find, installation into GAC is mainly intended to solve the problem of location of required assemblies (dependencies). If an assembly is installed into GAC, you may consider it exists "nearby" any application. It's like adding path to .exe to your PATH variable, but in "managed way". - of course, this is rather simplified description ;)

落在眉间の轻吻 2024-07-11 11:10:44

GAC 以完全信任方式运行,可供 Web 应用程序外部的应用程序使用。 例如,Sharepoint 中的计时器作业必须位于 GAC 中,因为 sptimer 服务是一个单独的进程。

“完全信任”部分也是安全问题的可能根源。 当然,您可以使用代码访问安全性,但不幸的是,我没有看到太多使用 CAS 的程序集 :( /bin 文件夹可以锁定到 Medium,这通常没问题。Daniel

Larson 有一个 也在 CAS 上发帖,其中详细介绍了差异。

The GAC runs with Full Trust and can be used by applications outside of your Web App. For example, Timer Jobs in Sharepoint HAVE to be in the GAC because the sptimer service is a separate process.

The "Full Trust" Part is also a possible source for security issues. Sure, you can work with Code Access Security, but I do not see too many Assemblies using CAS unfortunately :( The /bin Folder can be locked down to Medium which is normally fine.

Daniel Larson has a post on CAS as well which details the differences a bit more.

别理我 2024-07-11 11:10:44

需要提升权限才能代表不太受信任的代码(例如部分信任的 ASP.NET 应用程序)执行特权操作的程序集也可以使用 GAC。

例如,假设您有一个部分信任的 ASP.NET 应用程序,该应用程序需要执行需要提升权限的任务,即完全信任。 解决方案是将需要提升权限的代码放入单独的程序集中。 程序集用 AllowPartiallyTrustedCallers 属性标记,包含特权逻辑的类用 PermissionSet 属性标记,如下所示:

[PermissionSet(SecurityAction.Assert, Unrestricted=true)]

我们的程序集将被赋予一个强名称(签名),然后部署到广汽集团。

现在,我们部分受信任的应用程序可以利用 GAC 中的受信任程序集来执行一组特定且狭窄的特权操作,而不会失去部分信任的好处。

The GAC can also be used by assemblies that require elevated permissions to perform privileged operations on behalf of less trusted code (e.g. a partial trust ASP.NET application).

For example, say you have a partial trust ASP.NET application which needs to perform a task that would require elevated privileges, i.e. Full Trust. The solution is to put the code that requires elevated privileges into a separate assembly. The assembly is marked with the AllowPartiallyTrustedCallers attribute and the class that contains the privileged logic is marked with the PermissionSet attribute, something like this:

[PermissionSet(SecurityAction.Assert, Unrestricted=true)]

Our assembly would be given a strong name (signed) and then deployed into the GAC.

Now our partially trusted app(s) can utilise the trusted assembly in the GAC to carry out a specific and narrow set of privileged operations without losing the benefits of partial trust.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文