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

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

问题描述

我正在从.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 似乎存在版本冲突(请参阅下面的错误块.

尝试进行故障排除:

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

有人知道如何解决这些警告吗?任何帮助,我们将不胜感激.

关于以下警告之一的详细构建结果:

 第5419行:3>依赖关系"System.Collections.Concurrent,版本= 4.0.14.0,区域性=中性,PublicKeyToken = b03f5f7f11d50a3a".行5420:3>无法解决此参考.无法找到程序集"System.Collections.Concurrent,版本= 4.0.14.0,区域性=中性,PublicKeyToken = b03f5f7f11d50a3a".检查以确保程序集在磁盘上.如果您的代码需要此引用,则可能会出现编译错误.线5675:3>在"System.Collections.Concurrent,版本= 4.0.11.0,文化=中性,PublicKeyToken = b03f5f7f11d50a3a"和"System.Collections.Concurrent,版本= 4.0.14.0,文化=中性,PublicKeyToken = b03f5f7f11d50a3a"之间存在冲突.线5675:3>在"System.Collections.Concurrent,版本= 4.0.11.0,文化=中性,PublicKeyToken = b03f5f7f11d50a3a"和"System.Collections.Concurrent,版本= 4.0.14.0,文化=中性,PublicKeyToken = b03f5f7f11d50a3a"之间存在冲突.行5676:3>选择"System.Collections.Concurrent,版本= 4.0.11.0,文化=中性,PublicKeyToken = b03f5f7f11d50a3a",因为它是主要的,因此选择了"System.Collections.Concurrent,版本= 4.0.14.0,文化=中性,PublicKeyToken = b03f5f7f11d50a3a"不是.行5676:3>选择"System.Collections.Concurrent,版本= 4.0.11.0,文化=中性,PublicKeyToken = b03f5f7f11d50a3a",因为它是主要的,因此选择了"System.Collections.Concurrent,版本= 4.0.14.0,文化=中性,PublicKeyToken = b03f5f7f11d50a3a"不是.行5677:3>依赖于"System.Collections.Concurrent,版本= 4.0.11.0,区域性=中性,PublicKeyToken = b03f5f7f11d50a3a"的引用[C:\ Program Files \ dotnet \ sdk \ NuGetFallbackFolder \ netstandard.library \ 2.0.3 \ build \ netstandard2.0 \ ref \ System.Collections.Concurrent.dll].行5677:3>依赖于"System.Collections.Concurrent,版本= 4.0.11.0,区域性=中性,PublicKeyToken = b03f5f7f11d50a3a"的引用[C:\ Program Files \ dotnet \ sdk \ NuGetFallbackFolder \ netstandard.library \ 2.0.3 \ build \ netstandard2.0 \ ref \ System.Collections.Concurrent.dll]. 

更新

更多疑难解答:

请注意,在同一软件包中,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不可能,因为没有适用于GAC的.NET Core.NET核心.请参见 .NET Core是否有GAC等效项?
  3. 在.config中使用< bindingRedirect> .请参见重定向程序集版本|微软文档

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

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

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

解决方案

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天全站免登陆