如何防止 .NET 应用程序从 GAC 加载/引用程序集?
我能否以引用“本地”程序集(不在 GAC)而不是 GAC 内的程序集,尽管两个程序集具有相同的名称和相同的版本?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果两个程序集都是强命名(签名)的,则 CLR 将始终从 GAC 加载。
以下是运行时用于解析程序集引用的步骤(来自运行时如何定位程序集< /a>):
如同一篇文章后面所述:
因此,如果您有能力从本地程序集中删除签名,应用程序将使用它而不是 GAC 中的签名。
有关运行时绑定机制的更多详细信息,请参阅 苏珊娜·库克的博客。
Scott Hanselman 的这篇博客文章 还提供了绑定过程的精彩概述。
If both assemblies are strong-named (signed), the CLR will always load from the GAC.
Here are the steps the runtime uses to resolve assembly references (from How the Runtime Locates Assemblies):
As stated later in that same article:
So if you can afford to remove signing from the local assembly, the application will use it instead of the one in the GAC.
For more detail than you could probably ever want about the runtime-binding mechanisms, see Suzanne Cook's blog.
This blog entry from Scott Hanselman also provides a great overview of the binding process.
如果可以更改本地 dll 的版本号,则可以使用 oldVersion 属性来使用 dll 版本重定向。您可以为本地程序集使用强名称:
请看这个页面:
http://msdn.microsoft.com/en-us/library/7wd6ex19。 aspx
另外,您应该考虑可以修改已编译程序集的版本号,如下所述:
更改已编译的 .NET 程序集中的程序集版本
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
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
您可以使用 ilmerge 并将程序集合并到单个库中来解决它。
You can use ilmerge and merged the assemblies into a single library to get around it.
要成功部署 .NET Framework 应用程序,您必须了解公共语言运行时如何定位并绑定到组成应用程序的程序集。默认情况下,运行时尝试与构建应用程序所使用的程序集的确切版本进行绑定。此默认行为可以通过配置文件设置覆盖。
您可以使用程序集绑定日志查看器 (Fuslogvw.exe) 查看日志文件中的绑定信息,该查看器包含在 Windows 软件开发工具包 (SDK) 中。
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.
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).