如何解决 .NET Core 包版本冲突 [英] How to resolve .NET Core package version conflicts

查看:205
本文介绍了如何解决 .NET Core 包版本冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 .NET MVC 5 Web 应用程序迁移到 .NET Core 2.2 Web API 项目以及五个 .NET Standard 2.0 项目,这些项目都包含在一个解决方案中.

我现在收到 28 条关于包冲突的警告 (MSB3277),它们都在 System 命名空间内.例如,Version=4.0.11.0Version=4.0.14.0 之间的 System.Collections.Concurrent 似乎存在版本冲突(请参阅下面的错误块).

已尝试进行故障排除:

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

有谁知道如何解决这些警告?非常感谢任何帮助.

警告之一的详细构建输出:

第5419行:3>依赖关系System.Collections.Concurrent,Version=4.0.14.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a".第5420行:3>无法解析此引用.无法找到程序集System.Collections.Concurrent,Version=4.0.14.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a".检查以确保该程序集存在于磁盘上.如果您的代码需要此引用,您可能会遇到编译错误.第 5675 行:3>System.Collections.Concurrent, Version=4.0.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"和System.Collections.Concurrent, Version=4.0.14.0, Culture=neutral, PublicKeyToken=b03f5f7f3a3a"之间存在冲突.第 5675 行:3>System.Collections.Concurrent, Version=4.0.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"和System.Collections.Concurrent, Version=4.0.14.0, Culture=neutral, PublicKeyToken=b03f5f7f3a3a"之间存在冲突.第 5676 行:3>选择System.Collections.Concurrent,Version=4.0.11.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"是因为它是主要的,System.Collections.Concurrent,Version=4.0.14.0,Culture=neutral,PublicKeyToken=b03f5f7f3a3a"是不是.第 5676 行:3>选择System.Collections.Concurrent,Version=4.0.11.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"是因为它是主要的,System.Collections.Concurrent,Version=4.0.14.0,Culture=neutral,PublicKeyToken=b03f5f7f3a3a"是不是.第 5677 行:3>依赖于System.Collections.Concurrent,Version=4.0.11.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"的参考文献[C:Program FilesdotnetsdkNuGetFallbackFolder
etstandard.library2.0.3uild
etstandard2.0
efSystem.Collections.Concurrent.dll].第 5677 行:3>依赖于System.Collections.Concurrent,Version=4.0.11.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"的参考文献[C:Program FilesdotnetsdkNuGetFallbackFolder
etstandard.library2.0.3uild
etstandard2.0
efSystem.Collections.Concurrent.dll].

更新

更多故障排除:

请注意,在同一个包中,netcore50的汇编版本是4.0.10.0!

<块引用>

这是微软的错误吗?

不,这是一个功能.当程序集强命名时.,那么完整版本应该匹配.如果没有,这将给出警告.当不使用 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 核心.请参阅.NET Core 是否有等效的 GAC?
  3. 在您的 .config 中使用 .请参阅重定向程序集版本 |微软文档

    例如对于System.Collections.Concurrent":

    <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"属性中使用该版本

I am migrating from a .NET MVC 5 Web Application to a .NET Core 2.2 Web API project along with five .NET Standard 2.0 projects all housed under one solution.

I am now receiving 28 warnings (MSB3277) regarding package conflicts, which are all within the System namespace. For example, there appears to be a version conflict for System.Collections.Concurrent between Version=4.0.11.0 and Version=4.0.14.0 (see error block below).

Troubleshooting attempted:

  1. I tried uninstalling all of the .NET Core SDKs that I do not use, and currently have 2.2.300 installed. That did not work.
  2. I tried going into NuGet at the solution level to install the missing assembly, but 4.0.14 isn't even available in the selection list. I can select from the following versions: 4.3, 4.0.12, 4.0.10 and 4.0.0. I did try installing 4.3 in all of the projects, but that did not work.
  3. Other solutions on the web appear to either recommend going down a version on the SDK or hard-coding a reference in the *.csproj file - neither of which I prefer to do if I do not have to. I do not like the former because it seems counter-intuitive to be moving backwards and I do not like the latter because that hard codes a fix to permanently use an older package, which seems like a really bad idea because it seems unlikely that it will update to newer versions that get released.

Does anyone know how to resolve these warnings? Any help is greatly appreciated.

Detailed output on build for one of the warnings:

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 FilesdotnetsdkNuGetFallbackFolder
etstandard.library2.0.3uild
etstandard2.0
efSystem.Collections.Concurrent.dll].
Line 5677: 3>        References which depend on "System.Collections.Concurrent, Version=4.0.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" [C:Program FilesdotnetsdkNuGetFallbackFolder
etstandard.library2.0.3uild
etstandard2.0
efSystem.Collections.Concurrent.dll].

Update

More troubleshooting:

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

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

Is this a Microsoft bug?

Minimal output on build listing all 28 warnings:

3>C:Program Files (x86)Microsoft Visual Studio2019CommunityMSBuildCurrentBinMicrosoft.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 Studio2019CommunityMSBuildCurrentBinMicrosoft.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 Studio2019CommunityMSBuildCurrentBinMicrosoft.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 Studio2019CommunityMSBuildCurrentBinMicrosoft.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 Studio2019CommunityMSBuildCurrentBinMicrosoft.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 Studio2019CommunityMSBuildCurrentBinMicrosoft.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 Studio2019CommunityMSBuildCurrentBinMicrosoft.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 Studio2019CommunityMSBuildCurrentBinMicrosoft.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 Studio2019CommunityMSBuildCurrentBinMicrosoft.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 Studio2019CommunityMSBuildCurrentBinMicrosoft.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 Studio2019CommunityMSBuildCurrentBinMicrosoft.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 Studio2019CommunityMSBuildCurrentBinMicrosoft.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 Studio2019CommunityMSBuildCurrentBinMicrosoft.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 Studio2019CommunityMSBuildCurrentBinMicrosoft.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 Studio2019CommunityMSBuildCurrentBinMicrosoft.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 Studio2019CommunityMSBuildCurrentBinMicrosoft.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 Studio2019CommunityMSBuildCurrentBinMicrosoft.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 Studio2019CommunityMSBuildCurrentBinMicrosoft.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 Studio2019CommunityMSBuildCurrentBinMicrosoft.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 Studio2019CommunityMSBuildCurrentBinMicrosoft.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 Studio2019CommunityMSBuildCurrentBinMicrosoft.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 Studio2019CommunityMSBuildCurrentBinMicrosoft.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 Studio2019CommunityMSBuildCurrentBinMicrosoft.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 Studio2019CommunityMSBuildCurrentBinMicrosoft.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 Studio2019CommunityMSBuildCurrentBinMicrosoft.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 Studio2019CommunityMSBuildCurrentBinMicrosoft.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 Studio2019CommunityMSBuildCurrentBinMicrosoft.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.

解决方案

The issue is here strong naming and mixed assembly versions for at least "System.Collections.Concurrent", but probably for all the 28 warnings.

I tried going into NuGet at the solution level to install the missing assembly, but 4.0.14 isn't even available in the selection list.

The versions you are seeing are the NuGet versions, and those aren't the same to the assembly versions. e.g. NuGet version 4.3.0-preview1-24530-04 has assembly version 4.0.13.0 for .NET standard 1.3:

Please note that in the same package, the assembly version for netcore50 is 4.0.10.0!

Is this a Microsoft bug?

No it's a feature. When an assembly is strong named., then the full version should match. If not, this will give a warning. When not using the GAC, then only one version of the assembly could be published - so which one if multiple versions are used? Note- all Microsoft's assemblies are strong named.

The problem here:

  • You are including an (strong named) assembly that's built to System.Collections.Concurrent assembly version 4.0.14.0
  • You are (indirectly) using in your application assembly version 4.0.11.0 for the System.Collections.Concurrent assembly.

So two version for one assembly!

Solutions

There are multiple solutions possible:

  1. Use everywhere the same assembly version for System.Collections.Concurrent (you need to find out which NuGet package is assembly version 4.0.14.0). This is most of the time not feasible.
  2. Install the version 4.0.11.0 and 4.0.14.0 into the GAC - this ins't a really popular option either these days -> Not possible for .NET Core as there is no GAC for .NET Core. See Is there any GAC equivalent for .NET Core?
  3. Use a <bindingRedirect> in your .config. See Redirecting Assembly Versions | Microsoft Docs

    e.g. for "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>
    

Final notes

There are 28 version issues, but there is a big change that multiple issues are solved with a single bindingRedirect. So the best way is to start with one (preferable the most top-level one), and rebuild and repeat until all are resolved.

Please also note that downgrading an assembly is a bit tricky, e.g the assembly built on 4.0.13.0 could use a feature that is introduced/changed in 4.0.11.0+. So you could also update your version and use that version in "newVersion" attribute

这篇关于如何解决 .NET Core 包版本冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆