如何解决.NET Core软件包版本冲突

J W*_*ezy 16 c# conflicting-libraries .net-assembly visual-studio asp.net-core

我正在从.NET MVC 5 Web应用程序迁移到.NET Core 2.2 Web API项目以及五个都放在一个解决方案下的.NET Standard 2.0项目。

我现在收到有关程序包冲突的28条警告(MSB3277),这些警告都在System名称空间内。例如,似乎是一个版本冲突System.Collections.Concurrent之间Version=4.0.11.0Version=4.0.14.0(见差错块下文)。

尝试进行故障排除:

  1. 我尝试卸载所有我不使用的,当前已2.2.300安装的.NET Core SDK 。那没有用。
  2. 我尝试在解决方案级别进入NuGet来安装缺少的程序集,但是4.0.14在选择列表中甚至不可用。我可以从以下版本的选择:4.34.0.124.0.104.0.0。我确实尝试4.3在所有项目中进行安装,但这没有用。
  3. 网路上的其他解决方案似乎要么建议使用SDK上的版本,要么在* .csproj文件中对引用进行硬编码-如果不需要,我都不喜欢这样做。我不喜欢前者,因为向后移动似乎违反直觉,而我不喜欢后者,因为硬编码是永久使用较旧软件包的修复程序,这似乎是一个坏主意,因为它不太可能会更新为要发布的较新版本。

有谁知道如何解决这些警告?任何帮助是极大的赞赏。

有关警告之一的详细构建结果:

Line 5419: 3>    Dependency "System.Collections.Concurrent, Version=4.0.14.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
Line 5420: 3>        Could not resolve this reference. Could not locate the assembly "System.Collections.Concurrent, Version=4.0.14.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
Line 5675: 3>    There was a conflict between "System.Collections.Concurrent, Version=4.0.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" and "System.Collections.Concurrent, Version=4.0.14.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
Line 5675: 3>    There was a conflict between "System.Collections.Concurrent, Version=4.0.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" and "System.Collections.Concurrent, Version=4.0.14.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
Line 5676: 3>        "System.Collections.Concurrent, Version=4.0.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" was chosen because it was primary and "System.Collections.Concurrent, Version=4.0.14.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" was not.
Line 5676: 3>        "System.Collections.Concurrent, Version=4.0.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" was chosen because it was primary and "System.Collections.Concurrent, Version=4.0.14.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" was not.
Line 5677: 3>        References which depend on "System.Collections.Concurrent, Version=4.0.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" [C:\Program Files\dotnet\sdk\NuGetFallbackFolder\netstandard.library\2.0.3\build\netstandard2.0\ref\System.Collections.Concurrent.dll].
Line 5677: 3>        References which depend on "System.Collections.Concurrent, Version=4.0.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" [C:\Program Files\dotnet\sdk\NuGetFallbackFolder\netstandard.library\2.0.3\build\netstandard2.0\ref\System.Collections.Concurrent.dll].
Run Code Online (Sandbox Code Playgroud)

更新资料

更多故障排除:

https://github.com/dotnet/standard/issues/731

https://github.com/dotnet/corefx/issues/32561

这是Microsoft错误吗?

列出所有28条警告的构建体最小输出:

3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Collections.Concurrent" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Collections" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Collections.NonGeneric" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.ComponentModel" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Diagnostics.Debug" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Diagnostics.Process" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Diagnostics.Tracing" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.IO.FileSystem" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Linq" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Net.Primitives" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Net.Security" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Resources.ResourceManager" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Runtime" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Runtime.Extensions" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Runtime.InteropServices.RuntimeInformation" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Security.Cryptography.Algorithms" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Security.Cryptography.Csp" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Security.Cryptography.Encoding" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Security.Cryptography.Primitives" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Security.Cryptography.X509Certificates" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Text.Encoding.Extensions" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Text.RegularExpressions" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Threading" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Threading.Tasks" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Threading.ThreadPool" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Threading.Timer" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
3>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3277: Found conflicts between different versions of "System.Memory" that could not be resolved.  These reference conflicts are listed in the build log when log verbosity is set to detailed.
Run Code Online (Sandbox Code Playgroud)

Jul*_*ian 9

这里的问题是,至少为“ System.Collections.Concurrent”使用强命名和混合程序集版本,但可能是所有28条警告。

我尝试在解决方案级别进入NuGet来安装缺少的程序集,但是4.0.14在选择列表中甚至不可用。

您看到的版本是NuGet版本,这些版本与程序集版本不同。例如NuGet版本4.3.0-preview1-24530-04具有.NET标准1.3的程序集版本4.0.13.0:

请注意,在同一软件包中,netcore50的程序集版本为4.0.10.0!

这是Microsoft错误吗?

不,这是一个功能。当程序集的 名称为。时,完整版本应匹配。如果没有,这将给出警告。如果不使用GAC,则只能发布该程序集的一个版本-如果使用了多个版本,则发布哪个版本?注意-所有Microsoft的程序集都是强命名的。

这里的问题:

  • 您将包含一个基于System.Collections.Concurrent程序集版本4.0.14.0构建的(强名称)程序集
  • 您正在(间接)在应用程序程序集版本4.0.11.0中使用System.Collections.Concurrent程序集。

因此,一个组件有两个版本!

解决方案

可能有多种解决方案:

  1. 到处都使用与System.Collections.Concurrent相同的程序集版本(您需要找出哪个NuGet程序包是程序集版本4.0.14.0)。大多数时候这是不可行的。
  2. 将版本4.0.11.0和4.0.14.0安装到GAC中-如今这不是一个非常流行的选项 -> .NET Core不可能,因为没有.NET Core的GAC。请参阅.NET Core是否有等效的GAC?
  3. 使用<bindingRedirect>在你的.config。请参见重定向程序集版本。微软文档

    例如对于“ System.Collections.Concurrent”:

    <dependentAssembly>
        <assemblyIdentity name="System.Collections.Concurrent" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="4.0.0.0-4.0.14.0" newVersion="4.0.11.0" />
    </dependentAssembly>
    
    Run Code Online (Sandbox Code Playgroud)

最后的笔记

有28个版本问题,但是有一个很大的变化,即通过一个bindingRedirect解决了多个问题。因此,最好的方法是从一个开始(最好是最顶层的一个),然后重建并重复直到所有问题都解决为止。

还请注意,将程序集降级有点棘手,例如,在4.0.13.0上构建的程序集可以使用4.0.11.0+中引入/更改的功能。因此,您也可以更新版本,并在“ newVersion”属性中使用该版本