C# 如何防止 .NET 应用程序从 GAC 加载/引用程序集?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1606867/
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
How to prevent a .NET application from loading/referencing an assembly from the GAC?
提问by Flo
Can I configure a .NET application in a way (settings in Visual Studio) that it references a "local" assembly (not in GAC) instead of an assembly within the GAC, although both assemblies have the same name and the same version?
我可以通过引用“本地”程序集(不在GAC 中)而不是GAC中的程序集的方式(Visual Studio 中的设置)配置 .NET 应用程序,尽管这两个程序集具有相同的名称和相同的版本?
采纳答案by Jeff Sternal
If both assemblies are strong-named (signed), the CLR will always load from the GAC.
如果两个程序集都是强命名(签名),则 CLR 将始终从 GAC 加载。
Here are the steps the runtime uses to resolve assembly references (from How the Runtime Locates Assemblies):
以下是运行时用于解析程序集引用的步骤(来自运行时如何定位程序集):
Determines the correct assembly version by examining applicable configuration files, including the application configuration file, publisher policy file, and machine configuration file. If the configuration file is located on a remote machine, the runtime must locate and download the application configuration file first.
Checks whether the assembly name has been bound to before and, if so, uses the previously loaded assembly. If a previous request to load the assembly failed, the request fails immediately without attempting to load the assembly.
Checks the global assembly cache. If the assembly is found there, the runtime uses this assembly.
Probes for the assembly (... some material omitted ...)
通过检查适用的配置文件(包括应用程序配置文件、发布者策略文件和机器配置文件)来确定正确的程序集版本。如果配置文件位于远程机器上,则运行时必须首先找到并下载应用程序配置文件。
检查之前是否绑定了程序集名称,如果绑定,则使用之前加载的程序集。如果先前加载程序集的请求失败,则该请求会立即失败而不尝试加载程序集。
检查全局程序集缓存。如果在那里找到程序集,运行时将使用此程序集。
用于组装的探针(...省略了一些材料...)
As stated later in that same article:
正如同一篇文章后面所述:
There is no version checking for assemblies without strong names, nor does the runtime check in the global assembly cache for assemblies without strong names.
没有对没有强名称的程序集的版本检查,也没有在全局程序集缓存中对没有强名称的程序集进行运行时检查。
So if you can afford to remove signing from the local assembly, the application will use it instead of the one in the GAC.
因此,如果您负担得起从本地程序集中删除签名,应用程序将使用它而不是 GAC 中的签名。
For more detail than you could probably ever want about the runtime-binding mechanisms, see Suzanne Cook's blog.
有关运行时绑定机制的更多详细信息,请参阅Suzanne Cook 的博客。
This blog entry from Scott Hanselmanalso provides a great overview of the binding process.
Scott Hanselman 的这篇博客文章还提供了对绑定过程的很好的概述。
回答by BALKANGraph
To successfully deploy your .NET Framework application, you must understand how the common language runtime locates and binds to the assemblies that make up your application. By default, the runtime attempts to bind with the exact version of an assembly that the application was built with. This default behavior can be overridden by configuration file settings.
要成功部署 .NET Framework 应用程序,您必须了解公共语言运行时如何定位并绑定到构成应用程序的程序集。默认情况下,运行时会尝试与构建应用程序的程序集的确切版本进行绑定。配置文件设置可以覆盖此默认行为。
You can view binding information in the log file using the Assembly Binding Log Viewer (Fuslogvw.exe), which is included in the Windows Software Development Kit (SDK).
您可以使用程序集绑定日志查看器 (Fuslogvw.exe) 查看日志文件中的绑定信息,它包含在 Windows 软件开发工具包 (SDK) 中。
s
秒
回答by Paco Zarate
If you can change the version number of the local dll, you can use a dll version redirect using the oldVersion attribute. You can use a strong name for the local assembly: Please look this page: http://msdn.microsoft.com/en-us/library/7wd6ex19.aspx
如果可以更改本地 dll 的版本号,则可以使用使用 oldVersion 属性的 dll 版本重定向。您可以为本地程序集使用强名称:请查看此页面:http: //msdn.microsoft.com/en-us/library/7wd6ex19.aspx
Also you should consider that it is possible to modify the version number of a compiled assembly like it is described here: Change Assembly Version in a compiled .NET assembly
此外,您应该考虑可以像此处描述的那样修改已编译程序集的版本号: 在已编译的 .NET 程序集中更改程序集版本