Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve .NET Dll Hell?

Tags:

c#

.net

dll

How to fix?

I have 2 3rd party assemblies, that uses NewtonSoftJson.dll. The catch is one of them uses the older 3.x.x and another using 4.5.x. So at run time at least 1 of the 2 assemblies complains about the other.

How can I resolve this? I could set up services, but the codes and environments aren't currently set up that way. It's also too much refactoring than can be done safely in the given amount of time.

like image 540
Alwyn Avatar asked Feb 06 '13 22:02

Alwyn


2 Answers

I happened to have the exact problem with Newtonsoft and another third party library. The issue with Newtonsoft v3.x and v4.x is that the newer library now comes with a public key token. This made the assembly redirection solution useless; but it is a perfectly valid solution for most other cases.

I ended up reimplementing the third party library myself. If you have access to the source code of the third party library, you can always rebuild the library using the newer Newtonsoft DLL. You may need to make minor changes if any of the method signature changed.

like image 189
Adrian Godong Avatar answered Sep 22 '22 10:09

Adrian Godong


The Microsoft article "Redirecting Assembly Versions" has this to say:

The following example shows how to redirect one version of myAssembly to another, and turn off publisher policy for mySecondAssembly.

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="myAssembly"
          publicKeyToken="32ab4ba45e0a69a1"
          culture="en-us" />
        <!-- Assembly versions can be redirected in application, 
          publisher policy, or machine configuration files. -->
        <bindingRedirect oldVersion="1.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
      <assemblyIdentity name="mySecondAssembly"
        publicKeyToken="32ab4ba45e0a69a1"
        culture="en-us" />
        <!-- Publisher policy can be set only in the application 
          configuration file. -->
        <publisherPolicy apply="no" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
like image 23
Sam Axe Avatar answered Sep 21 '22 10:09

Sam Axe