使用 GAC 的优点和缺点是什么?
除此之外,是否存在必须使用全局程序集缓存或不能使用它的情况?
And on top of that, are there cases where one has to use the global assembly cache or where one cannot use it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
优点:
缺点:
建议:将 GAC 留给 MS 和朋友。 现在千兆字节很便宜。
Advantage:
Disadvantage:
Recommendation: Leave the GAC to MS and friends. The gigabyte is very cheap now.
总而言之,从简单开始,如果您稍后将程序集放入 GAC 和 NGEN 中看到性能大幅提升,那就继续吧,否则就不用费心了。 GAC更适合那些期望库在更多应用程序之间共享的框架,在99%的情况下,你不需要它。
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.
在我的一生中,我可能有一个应用程序,我必须将程序集放入 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.
我认为使用 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)
如果您要交付由多个程序集组成的可重用库,但只有少数程序集形成外观,并且该程序包已安装到开发人员的 PC 上,则可以考虑将程序集安装到 GAC 中。
想象一下,您运送了 6 个程序集,而这 6 个程序集中只有一个包含外观 - 即其他 5 个程序集仅由外观本身使用。 您运送:
使用您的项目的开发人员只想引用MyProduct.Facade.dll 在他们自己的项目中。 但是当他们的项目运行时,它必须能够递归地加载它引用的所有程序集。 如何实现这一目标? 一般来说,它们必须在 Bin 文件夹中或 GAC 中可用:
注意:最后一个选项并不要求您在将项目传送到生产 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:
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:
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 ;)
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.
需要提升权限才能代表不太受信任的代码(例如部分信任的 ASP.NET 应用程序)执行特权操作的程序集也可以使用 GAC。
例如,假设您有一个部分信任的 ASP.NET 应用程序,该应用程序需要执行需要提升权限的任务,即完全信任。 解决方案是将需要提升权限的代码放入单独的程序集中。 程序集用
AllowPartiallyTrustedCallers
属性标记,包含特权逻辑的类用 PermissionSet 属性标记,如下所示:我们的程序集将被赋予一个强名称(签名),然后部署到广汽集团。
现在,我们部分受信任的应用程序可以利用 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: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.