如何解决 .NET Core 包版本冲突
Posted
技术标签:
【中文标题】如何解决 .NET Core 包版本冲突【英文标题】:How to resolve .NET Core package version conflicts 【发布时间】:2019-10-30 03:33:31 【问题描述】:我正在从一个 .NET MVC 5 Web 应用程序迁移到一个 .NET Core 2.2 Web API 项目以及五个 .NET Standard 2.0 项目,它们都位于一个解决方案下。
我现在收到 28 条关于包冲突的警告 (MSB3277),它们都在 System
命名空间内。例如,Version=4.0.11.0
和Version=4.0.14.0
之间的System.Collections.Concurrent
似乎存在版本冲突(请参阅下面的错误块)。
已尝试故障排除:
-
我尝试卸载所有我不使用的 .NET Core SDK,目前安装了
2.2.300
。那没有用。
我尝试在解决方案级别进入 NuGet 以安装缺少的程序集,但选择列表中甚至没有 4.0.14
。我可以从以下版本中进行选择:4.3
、4.0.12
、4.0.10
和 4.0.0
。我确实尝试在所有项目中安装4.3
,但没有奏效。
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
这是微软的错误吗?
构建列出所有 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.
【问题讨论】:
您是否检查了网络配置中对这些版本的引用? 这对您有帮助吗youtube.com/watch?v=shn3gL_UJ38?第一条评论中有时间戳。在 (12:42) 是“审查 3rd 方 NuGet 包”。 @Matt 考虑到有问题的项目不使用任何第 3 方 NuGet 包,这似乎并不适用。这似乎是 .NET Standard 2.0 的问题。请查看我刚刚发布的更新。 按照警告说的做。将构建输出设置为详细。 @CodeCaster 如果您查看我的第一个代码块,您会发现我已经这样做了。第二个代码块只是一个带有警告的所有项目的列表。 【参考方案1】:这里的问题是至少针对“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!
这是微软的错误吗?
不,这是一项功能。当程序集为 strong named. 时,完整版本应匹配。如果没有,这将给出警告。不使用 GAC 时,只能发布程序集的一个版本 - 如果使用多个版本,则使用哪个版本?注意——所有微软的程序集都是强命名的。
这里的问题:
您正在包含一个(强命名)程序集,该程序集构建到 System.Collections.Concurrent 程序集版本 4.0.14.0 您正在(间接)在应用程序程序集版本 4.0.11.0 中使用 System.Collections.Concurrent 程序集。所以一个程序集有两个版本!
解决方案
有多种可能的解决方案:
-
对 System.Collections.Concurrent 使用相同的程序集版本(您需要找出哪个 NuGet 包是程序集版本 4.0.14.0)。这在大多数情况下是不可行的。
在您的 .config 中使用 <bindingRedirect>
。见Redirecting Assembly Versions | Microsoft Docs
例如对于“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>
结语
有 28 个版本问题,但有一个很大的变化是通过单个 bindingRedirect 解决了多个问题。所以最好的方法是从一个(最好是最顶层的)开始,然后重新构建并重复,直到所有问题都解决。
还请注意,降级程序集有点棘手,例如,基于 4.0.13.0 构建的程序集可以使用 4.0.11.0+ 中引入/更改的功能。因此,您还可以更新您的版本并在“newVersion”属性中使用该版本
【讨论】:
感谢您的详细解答。问题:通过将 bindingRedirect 应用到旧版本,这是否意味着我将永远使用这个旧包进行硬编码,从而无法自动升级到新版本?如果是这种情况,我是否需要在将来的某个时间手动升级它? 这是配置,所以没有硬编码。在部署另一个版本的 System.Collections.Concurrent 时 - 例如在未来的版本中 - 您需要更新 newVersion 属性以使版本匹配。 是否有可能不必这样做?根据其中一个 GitHub 链接,有人提到它似乎要到 .NET Standard 2.1 才能解决。与此同时,他们转而使用 .NET Core。如果可能的话,我会尽量避免这样做。但必要时会追究前者。 您可以尝试添加到您的 csprojs<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
,但不确定这是否适用于这种情况。
是的。在你的帮助下,我能够做到。谢谢你。 NuGet 包资源管理器帮助我揭开了它的神秘面纱 - 我不知道该工具,它需要一些工作才能将其安装在 Windows 7 上。无论如何,看到框架之间的差异暗示我复制的 using
语句过度给它带来了问题。因此,我在没有 using
语句的情况下重新迁移了项目,然后在智能感知的帮助下重新包含它们。【参考方案2】:
为每个程序集绑定重定向 (bindingRedirect),您可以在配置中手动执行此操作,或者在项目文件属性中有一个选项。 检查它然后升级。
【讨论】:
以上是关于如何解决 .NET Core 包版本冲突的主要内容,如果未能解决你的问题,请参考以下文章