10

What is the best practice in the .NET world to manage transitive dependencies that cause version conflict ?

In details : Project A Depends on Project B which in turn depends on library C

also

Project A also depends on Project X which depends on a DIFFERENT and (potentially) incompatible version of library C.

A->B->Cv1.0
&
A->X->Cv2.0
where
Cv1.0 <> Cv2.0

  • Is there a way to make this work ?

  • Can it be done WITHOUT using the GAC ?

  • Can it be done even if B and X are in binary format only (source not accessible) ?

In other words is there a way where I can have Project B and X each using their own dependencies when used together in Project A without causing conflicts.

NOTE: I realize that ideally I should not have this problem at all but as reliance on external libraries expands this will be an unavoidable side effect. So I am wondering should it occur how best to deal with it.

8
  • 1
    Ideally you should re-build or update B so that it depends on the current version of C. If you cannot do that you can try to re-map versions. stackoverflow.com/a/11126867/48082 This is not guaranteed to work!
    – Cheeso
    Jul 4, 2012 at 21:38
  • 2
    Agreed that ideally one should clean up their projects to avoid the situation in the firt place but this is not always possible, hence the question.
    – Newtopian
    Jul 4, 2012 at 21:49
  • 1
    remapping DLL in some way is good only if both versions are compatible with each-other. Worst case scenario here is that in the event they are not compatible is there a way to have each middle dependencies use the version it was made for and still perform the work the main project is expecting of them.
    – Newtopian
    Jul 4, 2012 at 21:51
  • yes, you can also load multiple distinct versions of an assembly. To do that, use distinct AppDomains. stackoverflow.com/a/1189491/48082
    – Cheeso
    Jul 4, 2012 at 21:53
  • 2
    The CLR can load the 2 versions inside the AppDomain (I already observed that fact, I guess referencing through strong-naming is enough). But by not refactoring/recompiling external dependencies you expose yourself to a lot of hard trouble. Solution that will work on your machine might not work in other exotic production environment. Jul 5, 2012 at 13:37

1 Answer 1

5

There are a lot of similar questions On Stack Overflow. For e.g. Referencing 2 different versions of log4net in the same solution

Summary:

  1. Ensure that you deploy the assembly C in folders 1.0 and 2.0 respectively within the folder containing the main executable.
  2. Change app.config file and include something like following:
 <configuration>
   <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
       <assemblyIdentity name="C" publicKeyToken="C's public key token" />
        <codeBase version="version string for C1.0 e.g. 1.0.0.0" href="1.0\C.dll" />
        <codeBase version="version string for C2.0 e.g. 2.0.0.0" href="2.0\C.dll" />
       </assemblyIdentity>
      </dependentAssembly>
    </assemblyBinding>
   </runtime>
 </configuration>

you can get the public key token of C using sn -T C.dll

If v1.0 and v2.0 of C have different public key (though ideally they shouldn't) then include two dependentAssembly tags.

4
  • 1
    there are indeed many questions but none really answered my question. My understanding of assemblyBinding was that it allows to specify which DLL is used for which namespace and to redirect calls accordingly. Here each call path will ultimately target a single DLL. In the scenario I exposed we have two identical call path that must target two different DLL. The only difference is that each is called from a different "parent" DLL. That is unless I did not understand correctly the purpose of assemblyBinding.
    – Newtopian
    Jul 5, 2012 at 12:37
  • Tha said I guess that if the publisher of the two dependencies (B and X in the example above) were to specifically hardwire the DLL dependency (C above) versions when producing their libraries everything would just work (provided both C Dll are uniquely identifiable files ie different name and/or paths). I will have to experiment a bit with assemblyBinding see what can be done
    – Newtopian
    Jul 5, 2012 at 12:41
  • If C is strongly named, above should work and should be able to load correct version of C for B and X. If it is not strongly named but still has a version (as specified through AssemblyVersion attribute) then you can experiment with removing the publicKeyToken and see if it still works. If that does not work, I think only hope would be AssemblyResolve event. I will try to check all these ifs and buts and share the results. Jul 5, 2012 at 14:09
  • Thanks Amit for you time and effort on this, Curious to see what you will find
    – Newtopian
    Jul 5, 2012 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.