解决Nuget依赖与dll的冲突不起作用,我做错了什么

问题描述 投票:0回答:1

我试图通过将冲突的库移动到各个 dll 来解决 Nuget 依赖项冲突,但它不起作用。是我做错了什么,还是我的方法不对?

这是一个 .Net 4.7.2 C# 项目。 Visual Studio 2022。

我的项目依赖两套第三方库;一些图像处理库(例如 OpenCVSharp)和一些 http 库(例如 System.Text.Json)。这些库具有冲突的 Nuget 依赖关系;它们各自依赖于相同 Nuget 包的不同版本。我已将所有与图像相关的内容移至 LibA.dll 中,并将 Nuget 依赖项安装到该 dll 项目中,效果非常好。我已将所有与 Web 相关的内容移至 LibB.dll 中,并将 Nuget 依赖项安装到该 dll 项目中,效果非常好。但是,当我尝试在同一个项目中使用这两个 dll 时,即使 Nuget 包安装在 dll 项目中而不是主应用程序项目中,依赖关系错误仍然出现。我认为将所有内容移动到单独的 dll 中可以解决这个问题,但我错了。难道我的做法全错了?是否可以在 C 中但在 C# 中执行相当于静态链接的操作,以使这些 dll 真正独立?欢迎任何建议!

c# .net dependencies nuget conflicting-libraries
1个回答
0
投票

对于我的情况,绑定重定向就是答案,上面来自 @iCodeSometime 的链接将带您了解详细信息。在运行时我收到此错误:

  System.AggregateException
  HResult=0x80131500
  Message=One or more errors occurred.
  Source=mscorlib
  StackTrace:
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at MyApp.PageEoLTest.CompileDataTestResults(BiometricData bioData, MatchTemplateTestData matchData, cmd_diag_v2_managed cmd_diag_v2, cmd_diag_v3_managed cmd_diag_v3, cmd_diag_v4_managed cmd_diag_v4, HFDiagCV22_managed cv22_diag, HFPadDiag_managed padData, EolTestData& etd) 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)

无论如何,就我而言,解决方案位于应用程序的 App.config 不是 dll 的 App.config) 添加以下内容,以强制应用程序加载版本 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>

注意“newVersion=7.0.0.0”。我遇到了一些错误,这种方法解决了所有这些错误。

© www.soinside.com 2019 - 2024. All rights reserved.