如何防止 .NET 应用程序从 GAC 加载/引用程序集?

发布于 2024-08-08 19:01:39 字数 183 浏览 3 评论 0原文

我能否以引用“本地”程序集(不在 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 技术交流群。

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

发布评论

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

评论(4

慢慢从新开始 2024-08-15 19:01:39

如果两个程序集都是强命名(签名)的,则 CLR 将始终从 GAC 加载。

以下是运行时用于解析程序集引用的步骤(来自运行时如何定位程序集< /a>):

  1. 通过检查适用的程序集来确定正确的程序集版本
    配置文件,包括
    应用程序配置文件,
    发布者策略文件和机器
    配置文件。如果
    配置文件位于
    远程机器,运行时必须
    找到并下载该应用程序
    首先配置文件。

  2. 检查程序集名称之前是否已绑定,如果是,
    使用之前加载的程序集。
    如果先前请求加载
    组装失败,请求失败
    立即而不尝试加载
    大会。

  3. 检查全局程序集缓存。如果在那里找到该程序集,则
    运行时使用此程序集。

  4. 装配探针(...省略了一些材料...)

如同一篇文章后面所述:

没有强名称的程序集没有版本检查,运行时也没有
检查全局程序集缓存中是否有没有强名称的程序集。

因此,如果您有能力从本地程序集中删除签名,应用程序将使用它而不是 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):

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

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

  3. Checks the global assembly cache. If the assembly is found there, the
    runtime uses this assembly.

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

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.

束缚m 2024-08-15 19:01:39

如果可以更改本地 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

安静 2024-08-15 19:01:39

您可以使用 ilmerge 并将程序集合并到单个库中来解决它。

You can use ilmerge and merged the assemblies into a single library to get around it.

柠檬心 2024-08-15 19:01:39

要成功部署 .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).

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