Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I force .NET to use a local copy of an assembly that's in the GAC

Tags:

.net

gac

I have a .NET assembly that (for reasons outside my control) must be in the GAC. However, the same assembly is used by another program, which has a its own copy of an older version of the same assembly. It must use its own copy and not whatever is in the GAC. Proper versioning is probably more hassle than it's worth in this case, for reasons I won't go into. My questions is: is there anyway to tell .NET: just use THIS DLL, right here in this directory - ignore whatever you find in the GAC or anywhere else.

like image 270
EMP Avatar asked Nov 06 '08 05:11

EMP


People also ask

How does .NET resolve assembly references?

If the runtime determines that an assembly matches the calling assembly's criteria, it uses that assembly. When the file specified by the given <codeBase> element is loaded, the runtime checks to make sure that the name, version, culture, and public key match the calling assembly's reference.


2 Answers

Make sure the GAC Assembly and local Assembly have different version numbers (not a bad idea to let your build number, at least, auto-increment by wild-carding your AssemblyVersion in AssemblyInfo: [assembly: AssemblyVersion("1.0.0.*")] ). Then, redirect your assembly binding using your app's config:

  • http://msdn.microsoft.com/en-us/library/2fc472t2(VS.80).aspx
  • http://msdn.microsoft.com/en-us/library/433ysdt1(VS.80).aspx.

In your case, you won't need the "appliesTo" attribute of the assemblyBinding config. You just need something like:

<runtime>     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">         <dependentAssembly>             <assemblyIdentity name="YourAssembly" publicKeyToken="AAAAAAAAAAAA" culture="neutral"/>             <bindingRedirect oldVersion="0.0.0.0-5.2.1.0" newVersion="5.0.8.1"/>         </dependentAssembly>     </assemblyBinding> </runtime> 
like image 122
Corbin March Avatar answered Sep 20 '22 07:09

Corbin March


If they have the same version number the answer is you can't. If you attempt to load an assembly that has the same full assembly name (name, version, key) as a GAC'd assembly the CLR will pick the GAC'd assembly every single time.

like image 23
JaredPar Avatar answered Sep 20 '22 07:09

JaredPar