Visual Studio 如何解决.NET Core包版本冲突

zdwk9cvp  于 2022-12-04  发布在  .NET
关注(0)|答案(3)|浏览(298)

我正在从一个.NET MVC 5 Web应用程序迁移到一个.NET Core 2.2 Web API项目沿着五个.NET Standard 2.0项目,所有这些项目都位于一个解决方案下。
我现在收到28个关于包冲突的警告(MSB 3277),这些警告都在System命名空间中。例如,Version=4.0.11.0Version=4.0.14.0之间似乎存在System.Collections.Concurrent的版本冲突(请参阅下面的错误块)。

已尝试疑难排解:

1.我试着卸载所有我不使用的.NET核心SDK,目前安装了2.2.300。但没有成功。
1.我尝试在解决方案级别进入NuGet来安装缺少的程序集,但是4.0.14甚至不在选择列表中。4.34.0.124.0.104.0.0。我确实尝试在所有项目中安装4.3,但没有成功。

  1. Web上的其他解决方案似乎要么建议在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].

更新

更多故障排除:
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.
ux6nzvsh

ux6nzvsh1#

这里的问题是强命名和至少“System.Collections.Concurrent”的混合程序集版本,但可能是所有28个警告。
我尝试在解决方案级别进入NuGet来安装缺少的程序集,但是4.0.14甚至不在选择列表中。
您看到的版本是NuGet版本,这些版本与程序集版本不同。例如,NuGet版本4.3.0-preview 1 -24530-04具有.NET标准1.3的程序集版本4.0.13.0:

请注意,在同一软件包中,netcore 50的程序集版本是4.0.10.0!
这是Microsoft错误吗?
不,它是一个功能。当程序集是strong named.时,完整版本应该匹配。如果不是,这将给予警告。当不使用GAC时,只能发布程序集的一个版本-如果使用多个版本,那么是哪一个?注意-所有Microsoft的程序集都具有强名称。
问题就在这里:

  • 您正在包含建置为System.Collections.Concurrent组件版本www.example.com的(强式名称)组件4.0.14.0
  • 您在应用程序组件版本www.example.com中(间接)4.0.11.0使用System.Collections.Concurrent组件。

所以一个组件有两个版本!

溶液

有多种可能的解决方案:
1.在任何地方使用System.Collections.Concurrent的相同程序集版本(您需要找出哪个NuGet包是程序集版本4.0.14.0)。这在大多数情况下是不可行的。
1.将4.0.11.0和4.0.14.0版本安装到GAC中--这在现在也不是一个很流行的选项--〉对于.NET核心不可能,因为.NET核心没有GAC。请参见Is there any GAC equivalent for .NET Core?
1.在. config中使用<bindingRedirect>。请参阅Redirecting Assembly Versions | Microsoft Docs
例如,对于“系统.收集.并发”:

<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>

最终注解

有28个版本问题,但有一个很大的变化,多个问题都可以通过一个bindingRedirect解决。所以最好的方法是从一个开始(最好是最顶级的),然后重建和重复,直到所有问题都解决。
还请注意,降级程序集有点棘手,例如,在www.example.com上构建的程序集4.0.13.0可以使用在www.example.com +中引入/更改的功能4.0.11.0。因此,您也可以更新版本并在“newVersion”属性中使用该版本

cyvaqqii

cyvaqqii2#

每个程序集的绑定重定向(bindingRedirect)可以在配置中手动执行,或者在项目文件属性中有一个选项。请检查它,然后升级。

5hcedyr0

5hcedyr03#

@Julian的解决方案对我不起作用。在我的例子中,我有一个测试项目引用了另一个具有业务逻辑的项目。依赖关系图如下所示:

Project.BL.Tests
↳ Project.BL

Project.BL
↳ Microsoft.Extensions.DependencyInjection.Abstractions Version="6.0.0"
↳ ThirdParty.Nuget.Package

ThirdParty.Nuget.Package
↳ Microsoft.Extensions.DependencyInjection.Abstractions Version="3.1.0"

当我试图在测试中创建一个ServiceCollection示例时,在构建时抛出了错误,指出DI.Abstractions v6和v3冲突。

<PropertyGroup>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>

但这并没有解决问题。
解决方案是将ThirdParty.Nuget.Package依赖项直接添加到测试项目:

Project.BL.Tests
↳ Project.BL
↳ ThirdParty.Nuget.Package

相关问题