Search code examples
c#.netdependenciesnugetconflicting-libraries

Solving Nuget dependency conflicts with dll's not working, what am I doing wrong


I'm trying to solve Nuget dependency conflicts by moving conflicting libraries to individual dll's, but it's not working. Am I doing something wrong, or is my approach incorrect?

This is a .Net 4.7.2 C# project. Visual Studio 2022.

My project depends on two sets of 3rd party libraries; some image processing libraries such as OpenCVSharp, and some http libraries such as System.Text.Json. These libraries have conflicting Nuget dependencies; they each depend on different versions of the same Nuget packages. I've moved everything image related into LibA.dll and installed the Nuget dependencies to that dll project, and it works great. I've moved everything web related into LibB.dll and installed the Nuget dependencies to that dll project, and it works great. But when I try to use both dll's in the same project, the dependency errors still appear, even though the Nuget packages are installed in the dll projects but not in the main app project. I thought that moving everything into individual dll's would solve it, but I was wrong. Is my approach all wrong? Is it possible to do the equivalent of static linking in C, but in C#, to make these dll's truly independent? Any and all suggestions are welcome!


Solution

  • So for my case, Binding Redirects was the answer, the link above from @iCodeSometime takes you to the details. At runtime I was getting this error:

      System.AggregateException
      HResult=0x80131500
      Message=One or more errors occurred.
      Source=mscorlib
      StackTrace:
       at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
       at MyApp.MyPage.MyMethod(MyDataClass data, MyOtherDataClass otherData,..edit_remove_proprietary_info) in C:\edit_propretary_info\somefile.xaml.cs:line 868
    
    Inner Exception 1:
    FileLoadException: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
    
    Inner Exception 2:
    FileLoadException: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=7.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
    

    The solution, for my case anyway, is in the App.config FOR THE APPLICATION (not the App.config for the dll) add the following, to force the application to load version 7.0.0.0:

      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Bcl.AsyncInterfaces" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="7.0.0.0" />
      </dependentAssembly>
    

    Note the "newVersion=7.0.0.0". I had a couple of these errors, and this approach resolved them all.