Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a specific version of a dll in C#

Tags:

c#

version

dll

gac

I have a project where I would want to use some specific version of a dll.

The GAC contains couple of versions of that dll (new & old), I would want to use the old when running the program.

Issue is that the newest dll is always picked-up from the GAC.

Would you know if there is a way to either:

  • Force the usage the dll that is in the run folder (the one I'm referencing in my solution, working fine in debug).
  • Force the usage of the old version of the dll from the GAC.

Thank you!

like image 383
goul Avatar asked Nov 19 '25 16:11

goul


1 Answers

You can use a binding redirect in your app.config or web.config in the runtime node:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
  </appSettings>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

Make sure you have the correct publicKeyToken and know which versions you want to redirect to what version.

(You can check a publicKeyToken of a DLL like this with this info.)

MSDN Documentation

You can also generate these for an entire solution using the Package Manager Console

Get-Project -All | Add-BindingRedirect

This will update all app.config files and add the binding redirect.

like image 158
Aage Avatar answered Nov 21 '25 05:11

Aage