Dll in both the bin and the gac, which one gets used?

Solution 1:

If it has the same version number as the referenced DLL, the GAC gets used.

If you increment the version number, rebuild the website referencing the new version number, put the new version in the /bin directory, then that DLL will be used.

If you do not want to change the version number, you're pretty much out of luck.

When .NET loads strong named assemblies, first it tries to decide what version number to use. It does this via the reference first, then it looks for publisher policies, then it looks for binding redirects in the configuration file.

After it does this, it looks for the assembly in the GAC, then in any codebase specified, then it probes various file system folders for the DLL. If at any one of those steps it finds the right version assembly, it stops.

If you are not changing the version number of your strong named assembly, .NET will find the original one in the GAC and stop looking. Note that because it stops when it finds one, and because looking in the GAC is first, specifying a codebase for your assembly will do no good unless you also specify a new version number.

Solution 2:

I have been able to override the GAC with the assembly in the \bin folder using the <codebase>Element.

By specifying <codebase version="1.2.3.4" href="/bin/MyAssembly.dll" /> in my web.config file I can tell my application to use this version rather than the version specified in the GAC.

You may also want to take a look at the <probing>Element for specifying assembly locations?

Solution 3:

I think I might be saying the same think as Adam Sills, but re-worded it for my understanding. Through my own testing, looks like this is what happens:

  • If your app is compiled with version 1.0.0.0 and 1.0.0.1 is in the GAC, then you can omit the .dll from your /bin.
  • If your app is compiled with version 1.0.0.1 and 1.0.0.0 is in the GAC, then you MUST place the .dll in your /bin to ignore the GAC. A error will occur if the GAC version is older than the required version of your app, unless you include the newer version in your /bin.

I hope this is correct...