How to prevent a .NET application from loading/referencing an assembly from the GAC?

37,677

Solution 1

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.

Solution 2

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

Solution 3

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

Share:
37,677
Nick Maina
Author by

Nick Maina

I'm a software engineer working and living in Munich, Germany. At the moment I'm focused on mobile web application development. Interests: Javascript, AngularJs, JAVA, IntelliJ, nginx, node.js, Maven, Git, Android, iOS,

Updated on September 25, 2020

Comments

  • Nick Maina
    Nick Maina over 3 years

    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?