Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force load an assembly from the /bin and not the GAC?

I have two assemblies, created through conditional compilation (dev and real).

The public surface of these assemblies is 100% identical: both are strongly named; both are signed with the same .snk and therefore have the same PublicKeyToken; both have the same culture and the same version. I cannot change this: making them appear identical is the whole point.

However, on my machine the real assembly is in the GAC. I have an ASP.NET 3.5 WebForms app that references the dev assembly. It absolutely must do that; the real assembly crashes the app.

Is there a way to force a specific ASP.NET application to use the dev one (which is in /bin), given that:

  • There is one in the GAC.
  • Both have the same Version and PublicKeyToken.
  • Both are strongly named/signed with the same key.
  • I can not change them, can't change the version, and can't remove the key.

I noticed that someone already asked this in #991293, but the accepted answer involved removing the signing, which isn't an option here.

Am I out of luck?

like image 206
Michael Stum Avatar asked Jun 09 '11 20:06

Michael Stum


People also ask

Why would I avoid the GAC?

The best answer was that "The GAC is only useful if you register libraries which you're going to reuse." In other words, don't use it if you are not going to share libraries between different applications.

How do I find out if assembly is registered in GAC?

If you dont care that the assembly is actually in the GAC, but just loadable on the machine (from the appdomain) you can just use LoadAssembly with the assemblies name (strong, common, full, etc). If the assembly can be loaded by Fusion it will be and then you will know it exists.

Why should I store my shared assemblies in the GAC?

The Global Assembly Cache (GAC) is a central repository for storing shared assemblies. The GAC allows multiple versions of the same assembly to be installed concurrently and also prevents different assembly vendors from overwriting each other's assemblies.


2 Answers

GAC is always tried first, when binding assemblies: How the Runtime Locates Assemblies

So no, you can't do this. However if you explain why you have such strange requirements there might be a different work around, you have not thought of.

like image 93
Andrew Savinykh Avatar answered Nov 05 '22 20:11

Andrew Savinykh


No there is no way to do this. When loading an assembly the CLR will check to see if a DLL with an equivalent strong name is present in the GAC. If there is a matching assembly in the GAC it will pick the GAC assembly every time. There is unfortunately no way to override this behavior.

like image 36
JaredPar Avatar answered Nov 05 '22 19:11

JaredPar