【问题标题】:Found conflicts between different versions of "System.Runtime.CompilerServices.Unsafe" that could not be resolved发现无法解决的“System.Runtime.CompilerServices.Unsafe”不同版本之间的冲突
【发布时间】:2020-10-19 01:44:45
【问题描述】:

这似乎是许多类似问题之一,但我在其他问题中找不到解决方案。

我直接跳到二进制日志:

这是ResolveAssemblyReference任务下的相应子树:

There was a conflict between "System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" and "System.Runtime.CompilerServices.Unsafe, Version=4.0.6.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
    "System.Runtime.CompilerServices.Unsafe, Version = 4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" was chosen because it was primary and "System.Runtime.CompilerServices.Unsafe, Version=4.0.6.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" was not.
    References which depend on "System.Runtime.CompilerServices.Unsafe, Version = 4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" [C:\Users\mkharitonov\.nuget\packages\system.runtime.compilerservices.unsafe\4.5.2\ref\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll].
    C:\Users\mkharitonov\.nuget\packages\system.runtime.compilerservices.unsafe\4.5.2\ref\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll
          Project file item includes which caused reference "C:\Users\mkharitonov\.nuget\packages\system.runtime.compilerservices.unsafe\4.5.2\ref\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll".
            C:\Users\mkharitonov\.nuget\packages\system.runtime.compilerservices.unsafe\4.5.2\ref\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll
        C:\Users\mkharitonov\.nuget\packages\microsoft.extensions.primitives\2.0.0\lib\netstandard2.0\Microsoft.Extensions.Primitives.dll
          Project file item includes which caused reference "C:\Users\mkharitonov\.nuget\packages\microsoft.extensions.primitives\2.0.0\lib\netstandard2.0\Microsoft.Extensions.Primitives.dll".
            C:\Users\mkharitonov\.nuget\packages\microsoft.extensions.primitives\2.0.0\lib\netstandard2.0\Microsoft.Extensions.Primitives.dll
            C:\Users\mkharitonov\.nuget\packages\microsoft.extensions.caching.abstractions\2.0.0\lib\netstandard2.0\Microsoft.Extensions.Caching.Abstractions.dll
            xyz.Common
            C:\xyz\tip\bin.link\xyz.Web.Framework.dll
            C:\xyz\tip\bin.link\xyz.Web.Platform.dll
            AccrualEngine
            DataProcessor
            xyz.BusinessAPI.Core
            xyz.DataManager
            DataSvc
            xyz.Data
            xyz.HtmlPlatform
            xyz.BusinessAPI
            DeviceServices
            GrossPayEngineDataProvider
            MultiApprovalProcessor
            Payroll.Core.Data
            PunchStatusReconciliationEngine
            RateEngine2
            RateEngineDataProviderServer
            RuleEngine
            RuleEngineDataProvider
            RuleEngineHelper
            SharpTop.Common.Security.MemberSession
            SharpTop.DB.DL
            WorkTimeCalculationEngine
            C:\Users\mkharitonov\.nuget\packages\microsoft.extensions.caching.memory\2.0.0\lib\netstandard2.0\Microsoft.Extensions.Caching.Memory.dll
            C:\Users\mkharitonov\.nuget\packages\microsoft.extensions.options\2.0.0\lib\netstandard2.0\Microsoft.Extensions.Options.dll
        C:\Users\mkharitonov\.nuget\packages\system.memory\4.5.3\lib\netstandard2.0\System.Memory.dll
          Project file item includes which caused reference "C:\Users\mkharitonov\.nuget\packages\system.memory\4.5.3\lib\netstandard2.0\System.Memory.dll".
            C:\Users\mkharitonov\.nuget\packages\system.memory\4.5.3\lib\netstandard2.0\System.Memory.dll
            C:\Users\mkharitonov\.nuget\packages\system.diagnostics.diagnosticsource\4.6.0\lib\net46\System.Diagnostics.DiagnosticSource.dll
            C:\Users\mkharitonov\.nuget\packages\microsoft.applicationinsights\2.10.0\lib\net46\Microsoft.ApplicationInsights.dll
            C:\Users\mkharitonov\.nuget\packages\xyz.engine.core.common\1.0.20260.1\lib\netstandard2.0\xyz.Engine.Core.Common.dll
            xyz.Common
            C:\xyz\tip\bin.link\xyz.Web.Framework.dll
            C:\xyz\tip\bin.link\xyz.Web.Platform.dll
            AccrualEngine
            DataProcessor
            xyz.BusinessAPI.Core
            xyz.DataManager
            DataSvc
            xyz.Data
            xyz.HtmlPlatform
            xyz.BusinessAPI
            DeviceServices
            GrossPayEngineDataProvider
            MultiApprovalProcessor
            Payroll.Core.Data
            PunchStatusReconciliationEngine
            RateEngine2
            RateEngineDataProviderServer
            RuleEngine
            RuleEngineDataProvider
            RuleEngineHelper
            SharpTop.Common.Security.MemberSession
            SharpTop.DB.DL
            WorkTimeCalculationEngine
        C:\xyz\tip\Tools\MSBuild\..\..\bin.link\System.Threading.Tasks.Extensions.dll
          Project file item includes which caused reference "C:\xyz\tip\Tools\MSBuild\..\..\bin.link\System.Threading.Tasks.Extensions.dll".
            GrossPayEngineDataProvider
        C:\xyz\tip\Tools\MSBuild\..\..\bin.link\Pipelines.Sockets.Unofficial.dll
          Project file item includes which caused reference "C:\xyz\tip\Tools\MSBuild\..\..\bin.link\Pipelines.Sockets.Unofficial.dll".
            GrossPayEngineDataProvider
    References which depend on "System.Runtime.CompilerServices.Unsafe, Version = 4.0.6.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" [C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\System.Runtime.CompilerServices.Unsafe.dll].

据此我得出结论,System.Runtime.CompilerServices.Unsafe.dll 有两个版本在起作用:

  1. NuGet 包 - C:\Users\mkharitonov.nuget\packages\system.runtime.compilerservices.unsafe\4.5.2\ref\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll强>
  2. C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\System.Runtime.CompilerServices.Unsafe.dll

二进制日志非常详细地显示了哪些程序集依赖于 NuGet 包,但对于第二个参考,它没有显示任何内容。这很奇怪,因为那么问题是 - 为什么首先考虑第二个版本?

有问题的项目只是一个库,因此没有 app.config 文件。另外,AutoUnify == true 所以ResolveAssemblyReference 任务应该会自动将冲突解决到最高版本,至少这是我对documentation 的解释。

所以,我很困惑。我知道警告是次要的,可以忽略,但我想了解它的性质。如果显然所有依赖项都依赖于 NuGet 包,是什么原因造成的。或者,该任务可能不会输出某种依赖关系,而是由我来调查依赖于 C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\System 的内容。 Runtime.CompilerServices.Unsafe.dll ?

记录在案:

  1. NuGet包的汇编版本是4.0.4.1,第二个版本是4.0.6.0,所以不应该都解析到最高版本,即第二个版本吗?
  2. 我的代码都没有直接引用 System.Runtime.CompilerServices.Unsafe,因此ResolveAssemblyReference 任务列出的所有依赖项都必须是可传递的。
  3. 我尝试使用绑定重定向将 app.config 文件添加到有问题的项目中,尽管由于存在 AutoGenerateBindingRedirects 属性而应该不需要这样做。正如我所料 - 它没有帮助。

我如何确定发生了什么以及如何解决它?

【问题讨论】:

  • 我们遇到了同样的错误。在我们将 Visual Studio 19 更新到构建服务器上的最新版本之后。检查:github.com/xamarin/xamarin-android/issues/4872
  • 我的 msbuild 版本是 16.7.0+b89cb5fde。我想我必须升级到最新版本才能验证。
  • 我对 msbuild 16.9.0+57a23d249 也有同样的问题。没有 app.config 文件。项目不直接引用该文件,只是间接引用。由于 nuget 恢复了不同的项目(包含所有 nuget 包的超级项目),正确版本的文件已经在输出目录中。即便如此,msbuild 在 _CopyFilesMarkedCopyLocal(来自 C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\System.Runtime.CompilerServices 期间用旧版本覆盖输出目录中文件的正确版本。不安全.dll)。恼人的。尝试了一切,但没有成功。
  • 对我来说这是解决方案,全局禁用本地复制(我把它放在 Directory.Build.targets 中):thomasfreudenberg.com/archive/2012/11/21/…

标签: c# .net msbuild nuget


【解决方案1】:

我有一个更新。 在我们这边,app.config 中有一个条目重定向到 4.0.6.0。

我们从未见过这个问题,因为构建机器上没有 4.0.6.0。

但是当我们将 Visual Studio 更新到最新版本时,msbuild 不知何故复制了它。

_CopyFilesMarkedCopyLocal:
  The File will be copied from "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\System.Runtime.CompilerServices.Unsafe.dll" in "D:\agent_03\_work\21\s\Release\System.Runtime.CompilerServices.Unsafe.dll".

一旦我们删除了配置文件中的条目,该文件就不再被复制了。

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.6.0" newVersion="4.0.6.0" />
      </dependentAssembly>
</assemblyBinding>

在您的 app.configs 上搜索任何特定条目。如果找不到,恐怕您需要遍历所有 nuget 依赖项,直到找到哪个需要此特定版本。

干杯, 马可

【讨论】:

【解决方案2】:

我在构建解决方案时突然遇到了同样的错误。我恢复了备份并尝试了构建。这再次起作用,但是在复制更新的代码 sn-ps 后,错误又回来了。更新 Unsafe.dll 包,删除所有引用,更新 V​​isual Studio 等都没有帮助。它总是以某种方式再次恢复不正确的库。

通过纯粹的改变,我发现了原因。它与Linq 声明有关。看起来像这样

MyList.ForEach(x => x.property = x.property.Replace("ABC", MyOtherList.Where(x => x.property == SomeValue).First().name)));

这不会导致错误列表中出现错误。但是这一行导致库Unsafe.dll 一次又一次地添加到项目中。并且构建失败。 将第二个 Linq 部分 x 更改为 y 修复了所有问题。

MyList.ForEach(x => x.property = x.property.Replace("ABC", MyOtherList.Where(y => y.property == SomeValue).First().name)));

也许这对其他人有帮助,因为这个简单的问题花费了我大约 3 个小时的额外工作。

【讨论】:

    【解决方案3】:

    我在解决方案中遇到了类似的问题。原来我不知何故有这个库的 3 个不同版本。使用率最高的是 4.7.1,所以我升级了所有这些并修复了它。如果您有一个包含多个项目的解决方案,请使用 NuGet 包管理器查看在您的其他项目中使用的这个库,看看这是否是它把它放在日志中的原因

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 2014-09-06
    • 2017-09-20
    • 2010-12-24
    相关资源
    最近更新 更多