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

25

我正在从一个.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文件中硬编码引用——如果不是必须的话,我都不喜欢这样做。我不喜欢前者,因为它似乎逆向思维,而且我不喜欢后者,因为它会将一个旧软件包永久性地硬编码到代码中修复,这似乎是个很糟糕的想法,因为它不太可能更新到发布的新版本。

有人知道如何解决这些警告吗?非常感谢任何帮助。

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

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

这是一个微软的 bug 吗?

构建时列出所有 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.

1
按照警告所说的做。将构建输出设置为详细模式。 - CodeCaster
1
@CodeCaster 如果你看看我的第一个代码块,你会发现我已经做到了这一点。第二个代码块只是一个带有警告的所有项目的列表。 - J Weezy
长话短说,您需要手动删除所有这些包,然后仅添加成功编译所需的内容。删除所有 System.* 包。您可能需要按特定顺序执行此操作,因为某些包依赖于其他包,但通常,添加 System.ValueTuplesSystem.Collections.Concurrent 的唯一原因是兼容性包。 - Panagiotis Kanavos
到了那个时候,您会意识到最好开始一个新的ASP.NET Core项目,仔细复制您想要保留的代码,或者适应已经更改的代码,包括控制器、页面等。 - Panagiotis Kanavos
抱歉带来不好的消息,但我已经为此与警告和更糟糕的失败部署作斗争两年了。微软本身承认将.NET Standard 2.0兼容性改装到旧框架上是行不通的,这意味着每次升级包时没有问题的唯一完整框架版本是4.7.2和4.8。 - Panagiotis Kanavos
显示剩余5条评论
3个回答

20
问题在于至少对于“System.Collections.Concurrent”,强名称和混合程序集版本存在问题,但可能适用于所有28个警告。
我尝试进入NuGet解决方案级别安装丢失的程序集,但选择列表中甚至没有4.0.14可用。
您看到的版本是NuGet版本,它们与程序集版本不同。例如,.NET标准1.3的NuGet版本4.3.0-preview1-24530-04具有程序集版本4.0.13.0:

请注意,在同一个软件包中,netcore50的程序集版本为4.0.10.0!
“这是微软的一个bug吗?” 不是,这是一个特性。当程序集strong named时,完整版本应该匹配。否则,这将会产生一个警告。如果不使用全局程序集缓存,则只能发布程序集的一个版本 - 如果使用多个版本,哪一个版本呢?请注意,所有微软的程序集都是强命名的。
问题在于: - 您正在包含一个构建到System.Collections.Concurrent程序集版本4.0.14.0的(强命名)程序集。 - 您在应用程序中(间接)使用了System.Collections.Concurrent程序集版本4.0.11.0。
因此,一个程序集有两个版本!
解决方案: 有多种解决方案可供选择:
  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>
    

最后说明

有28个版本问题,但很可能多个问题可以通过单个bindingRedirect解决。因此,最好的方法是从一个问题开始(最好是最高级别的问题),然后重建和重复,直到所有问题都得到解决。

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


谢谢您提供详细的回答。问题:通过应用bindingRedirect到旧版本,这是否意味着我将永远使用这个旧包的硬编码,并因此无法自动升级到新版本?如果是这种情况,那么我将需要在未来某个时候手动升级它吗? - J Weezy
这是配置文件,因此没有硬编码。当部署另一个 System.Collections.Concurrent 的版本时(例如在未来的发布中),您需要更新 newVersion 属性以使版本匹配。 - Julian
有没有可能避免做这件事呢?根据 GitHub 上的一篇链接,个人提到这似乎不会得到解决,直到 .NET Standard 2.1。在此期间,他们转而使用 .NET Core。如果可能的话,我正在尝试避免做任何一种选择。但如果必要的话,我将追求前者。 - J Weezy
你可以尝试在csprojs中添加以下代码:<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects><GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>,但不确定这是否适用于此情况。 - Julian
是的。在您的帮助下,我成功地完成了它。谢谢。NuGet包资源管理器帮助我解决了问题 - 我之前不知道这个工具,并且在Windows 7上安装它需要一些操作。无论如何,通过查看不同框架之间的差异,我发现复制过来的“using”语句导致了问题。因此,我重新迁移了项目,没有包含“using”语句,然后使用Intellisense重新添加了它们。 - J Weezy

0

@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

0

为每个程序集执行绑定重定向 (bindingRedirect ),你可以手动在配置文件中进行操作,或者在项目文件属性中选择选项。检查后进行升级。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接