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

时间:2021-11-04 02:46:09

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?

我是否可以在某种方式(Visual Studio中的设置)中配置.NET应用程序,它引用“本地”程序集(而不是GAC)而不是GAC中的程序集,尽管两个程序集具有相同的名称和相同的版本?

4 个解决方案

#1


22  

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

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

  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.

因此,如果您能够从本地程序集中删除签名,则应用程序将使用它而不是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 Hanselman also provides a great overview of the binding process.

Scott Hanselman的博客文章也提供了绑定过程的精彩概述。

#2


4  

Did you see this answer?
Forcing the ASP.NET Application to load the assembly from bin not from GAC

你看到了这个答案吗?强制ASP.NET应用程序从bin而不是从GAC加载程序集

It sounds like you are trying to do the same thing.

听起来你正在尝试做同样的事情。

#3


1  

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程序集中更改程序集版本

#4


-1  

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

您可以使用Windows软件开发工具包(SDK)中包含的程序集绑定日志查看器(Fuslogvw.exe)在日志文件中查看绑定信息。

s

小号

#1


22  

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

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

  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.

因此,如果您能够从本地程序集中删除签名,则应用程序将使用它而不是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 Hanselman also provides a great overview of the binding process.

Scott Hanselman的博客文章也提供了绑定过程的精彩概述。

#2


4  

Did you see this answer?
Forcing the ASP.NET Application to load the assembly from bin not from GAC

你看到了这个答案吗?强制ASP.NET应用程序从bin而不是从GAC加载程序集

It sounds like you are trying to do the same thing.

听起来你正在尝试做同样的事情。

#3


1  

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程序集中更改程序集版本

#4


-1  

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

您可以使用Windows软件开发工具包(SDK)中包含的程序集绑定日志查看器(Fuslogvw.exe)在日志文件中查看绑定信息。

s

小号