使用GAC有哪些优缺点?

时间:2021-11-25 22:54:50

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

最重要的是,是否有必须使用全局程序集缓存或无法使用它的情况?

8 个解决方案

#1


43  

  • Loading assemblies from GAC mean less overhead and security that your application will always load correct version of .NET library
  • 从GAC加载程序集意味着更少的开销和安全性,您的应用程序将始终加载正确版本的.NET库
  • 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.
  • 您不应该使用GAC之外的程序集,因为几乎没有性能提升,在许多情况下甚至会降低性能。
  • You're already using GAC, because all standard .NET assemblies are actually in GAC and ngened (during installation).
  • 您已经在使用GAC,因为所有标准.NET程序集实际上都在GAC中并且在安装期间(在安装期间)。
  • Using GAC for your own libraries adds complexity into deployment, I would try to avoid it at all costs.
  • 将GAC用于您自己的库会增加部署的复杂性,我会不惜一切代价避免它。
  • 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.
  • 如果您想将某些内容放入GAC,则您的用户需要在安装期间以管理员身份登录,这对于许多类型的应用程序来说都是一个问题。

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和NGEN中,如果你以后看到主要的性能提升,那就去吧,否则不要打扰。 GAC更适合于希望在更多应用程序之间共享库的框架,在99%的情况下,您不需要它。

#2


19  

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.

建议:将GAC留给MS和朋友。技嘉现在非常便宜。

#3


18  

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

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

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:

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

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

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

我们的程序集将具有强名称(已签名),然后部署到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.

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

#4


10  

When i researched this topic myself, i found that Demystifying the .NET Global Assembly Cache By Jeremiah Talkar helped me a lot.

当我自己研究这个话题时,我发现通过Jeremiah Talkar揭开.NET全局汇编缓存的神秘面孔对我帮助很大。

#5


7  

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.

如果您要运送包含多个程序集的可重用库,但只有少数几个构成外观,则可以考虑将程序集安装到GAC中(如果程序包已安装到开发人员的PC中)。

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:

想象一下,你运送6个组件,这6个组件中只有一个包含一个外观 - 即其他5个仅由外观本身使用。你发货:

  • MyProduct.Facade.dll - that's the only component intended to be used by developers
  • MyProduct.Facade.dll - 这是开发人员打算使用的唯一组件
  • MyProduct.Core.dll - used by MyProduct.Facade.dll, but not intended to be used by developers
  • MyProduct.Core.dll - 由MyProduct.Facade.dll使用,但不打算供开发人员使用
  • MyProduct.Component1.dll - the same
  • MyProduct.Component1.dll - 相同
  • MyProduct.Component2.dll - the same
  • MyProduct.Component2.dll - 相同
  • ThirdParty.Lib1.dll - third-party library used by MyProduct.Component1.dll
  • ThirdParty.Lib1.dll - MyProduct.Component1.dll使用的第三方库
  • ThirdParty.Lib2.dll - the same
  • ThirdParty.Lib2.dll - 相同
  • 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:

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

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

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.

注意:最后一个选项不会让您在将项目运送到生产PC时也这样做。您可以在Bin文件夹中运送所有程序集,也可以将它们安装到GAC中 - 所有这些都取决于您的所有愿望。

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

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

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

#6


6  

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)

我认为使用GAC的最大优势之一是您可以为您的应用程序注册并提供相同程序集的多个版本。就个人而言,我不喜欢它如何限制机器之间的移动(我不喜欢说,检查新的VPC上的源代码并通过一系列步骤让它运行,因为我必须注册的东西GAC)

#7


6  

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.

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

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.

“完全信任”部分也是安全问题的可能来源。当然,你可以使用代码访问安全性,但我不会看到太多使用CAS的程序集:( / bin文件夹可以锁定到中,这通常很好。

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

Daniel Larson也有一篇关于CAS的文章,其中详细介绍了差异。

#8


3  

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中放置一个程序集,只是因为这些程序集是许多应用程序将使用它的框架的一部分,并且将它们放入GAC似乎是正确的。

#1


43  

  • Loading assemblies from GAC mean less overhead and security that your application will always load correct version of .NET library
  • 从GAC加载程序集意味着更少的开销和安全性,您的应用程序将始终加载正确版本的.NET库
  • 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.
  • 您不应该使用GAC之外的程序集,因为几乎没有性能提升,在许多情况下甚至会降低性能。
  • You're already using GAC, because all standard .NET assemblies are actually in GAC and ngened (during installation).
  • 您已经在使用GAC,因为所有标准.NET程序集实际上都在GAC中并且在安装期间(在安装期间)。
  • Using GAC for your own libraries adds complexity into deployment, I would try to avoid it at all costs.
  • 将GAC用于您自己的库会增加部署的复杂性,我会不惜一切代价避免它。
  • 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.
  • 如果您想将某些内容放入GAC,则您的用户需要在安装期间以管理员身份登录,这对于许多类型的应用程序来说都是一个问题。

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和NGEN中,如果你以后看到主要的性能提升,那就去吧,否则不要打扰。 GAC更适合于希望在更多应用程序之间共享库的框架,在99%的情况下,您不需要它。

#2


19  

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.

建议:将GAC留给MS和朋友。技嘉现在非常便宜。

#3


18  

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

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

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:

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

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

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

我们的程序集将具有强名称(已签名),然后部署到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.

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

#4


10  

When i researched this topic myself, i found that Demystifying the .NET Global Assembly Cache By Jeremiah Talkar helped me a lot.

当我自己研究这个话题时,我发现通过Jeremiah Talkar揭开.NET全局汇编缓存的神秘面孔对我帮助很大。

#5


7  

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.

如果您要运送包含多个程序集的可重用库,但只有少数几个构成外观,则可以考虑将程序集安装到GAC中(如果程序包已安装到开发人员的PC中)。

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:

想象一下,你运送6个组件,这6个组件中只有一个包含一个外观 - 即其他5个仅由外观本身使用。你发货:

  • MyProduct.Facade.dll - that's the only component intended to be used by developers
  • MyProduct.Facade.dll - 这是开发人员打算使用的唯一组件
  • MyProduct.Core.dll - used by MyProduct.Facade.dll, but not intended to be used by developers
  • MyProduct.Core.dll - 由MyProduct.Facade.dll使用,但不打算供开发人员使用
  • MyProduct.Component1.dll - the same
  • MyProduct.Component1.dll - 相同
  • MyProduct.Component2.dll - the same
  • MyProduct.Component2.dll - 相同
  • ThirdParty.Lib1.dll - third-party library used by MyProduct.Component1.dll
  • ThirdParty.Lib1.dll - MyProduct.Component1.dll使用的第三方库
  • ThirdParty.Lib2.dll - the same
  • ThirdParty.Lib2.dll - 相同
  • 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:

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

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

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.

注意:最后一个选项不会让您在将项目运送到生产PC时也这样做。您可以在Bin文件夹中运送所有程序集,也可以将它们安装到GAC中 - 所有这些都取决于您的所有愿望。

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

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

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

#6


6  

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)

我认为使用GAC的最大优势之一是您可以为您的应用程序注册并提供相同程序集的多个版本。就个人而言,我不喜欢它如何限制机器之间的移动(我不喜欢说,检查新的VPC上的源代码并通过一系列步骤让它运行,因为我必须注册的东西GAC)

#7


6  

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.

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

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.

“完全信任”部分也是安全问题的可能来源。当然,你可以使用代码访问安全性,但我不会看到太多使用CAS的程序集:( / bin文件夹可以锁定到中,这通常很好。

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

Daniel Larson也有一篇关于CAS的文章,其中详细介绍了差异。

#8


3  

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中放置一个程序集,只是因为这些程序集是许多应用程序将使用它的框架的一部分,并且将它们放入GAC似乎是正确的。