簡體   English   中英

.NET Core package版本沖突如何解決

[英]How to resolve .NET Core package version conflicts

我正在從一個 .NET MVC 5 Web 應用程序遷移到一個 .NET 核心 2.2 Web API 項目以及五個 .NET 標准 2.0 項目,所有項目都位於一個解決方案下。

我現在收到 28 條關於 package 沖突的警告 (MSB3277),它們都在System命名空間內。 例如,System.Collections.Concurrent 在Version=4.0.11.0Version=4.0.14.0 System.Collections.Concurrent之間似乎存在版本沖突(參見下面的錯誤塊)。

故障排除嘗試:

  1. 我嘗試卸載所有我不使用的 .NET 核心 SDK,目前安裝了2.2.300 那沒有用。
  2. 我嘗試在解決方案級別進入 NuGet 以安裝缺少的程序集,但選擇列表中甚至沒有4.0.14 我可以從以下版本 select : 4.34.0.124.0.104.0.0 我確實嘗試在所有項目中安裝4.3 ,但這沒有用。
  3. web 上的其他解決方案似乎要么建議降低 SDK 上的版本,要么在 *.csproj 文件中對引用進行硬編碼——如果沒有必要,我都不想這樣做。 我不喜歡前者,因為向后移動似乎違反直覺,我不喜歡后者,因為硬編碼修復永久使用舊的 package,這似乎是一個非常糟糕的主意,因為它似乎不太可能更新到發布的較新版本。

有誰知道如何解決這些警告? 任何幫助是極大的贊賞。

詳細 output on build 警告之一:

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/do.net/standard/issues/731

https://github.com/do.net/corefx/issues/32561

這是微軟的錯誤嗎?

構建中列出所有 28 個警告的最小 output:

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.

這里的問題是強大的命名和混合程序集版本至少為“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!

這是微軟的錯誤嗎?

不,這是一個功能。 當一個程序集強名為 。時,那么完整版本應該匹配。 如果沒有,這將發出警告。 不使用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 Core是否有任何GAC等效項?
  3. 在.config中使用<bindingRedirect> 請參閱重定向組件版本| 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),您可以在config中手動執行,或者在項目文件屬性中有一個選項。 檢查然后升級。

簡答:當從一個框架遷移到另一個框架時, 您可以復制並保留您的代碼,但不要復制和粘貼USING語句在課程頂部編譯器對后者不滿。 這是有道理的,因為有問題的框架(.NET Framework到.NET Core和.NET Standard)是不同的,並且在沒有詳細說明的情況下,微軟已經改變了方向。

Julian的回答指出了復制和粘貼using語句時的解決方法。 但是,我選擇繼續並在沒有using語句的情況下重新遷移項目,然后在Intellisense的幫助下手動重新包含它們。 所有警告都消失了。

嘆了口氣,這就是我復制和粘貼代碼所得到的。 看來微軟將.NET Framework遷移到.NET Core和.NET Standard實現的努力使得編譯器確信喝了五分之一的龍舌蘭酒並開始向開發人員講西班牙語。 就我而言,它說“¿que es eso?”

我的回答:你應該知道你是編譯器。

@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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM