You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何排查NuGet包中System.Runtime.CompilerServices.Unsafe版本冲突问题

问题根源定位
  • 冲突本质:你的项目直接引用了5.0.0版本的System.Runtime.CompilerServices.Unsafe,MSBuild默认优先选择直接引用的主版本(即警告中提到的was chosen because it was primary),但项目内多个间接依赖的第三方包要求使用6.0.0版本的该库,因此触发版本冲突。你的推测完全正确,就是项目直接安装的该库版本,和其他第三方包内部依赖的特定版本发生了冲突。
  • 双方依赖来源可直接从警告日志中提取:
    1. 5.0.0版本来源:项目一级直接安装了5.0.0版本的System.Runtime.CompilerServices.Unsafe NuGet包
    2. 6.0.0版本来源:以下间接依赖包底层均依赖6.0.0版本的该库:System.Memory 4.5.4、System.Collections.Immutable 5.0.0、全系列Nito.AsyncEx相关包、System.Threading.Tasks.Extensions 4.6.0预览版、Utf8Json 1.3.7、Easy.Logger.Extensions 1.5.0。
解决方案

优先推荐版本统一方案,兼容性最好:

  • 直接将项目中引用的System.Runtime.CompilerServices.Unsafe升级到6.0.0版本,全项目版本统一后冲突会直接消失。

如果因业务要求必须保留5.0.0版本,可通过绑定重定向解决:

  • 非SDK风格项目(传统.NET Framework项目):在app.config或web.config的<runtime><assemblyBinding>节点下添加如下配置:
<dependentAssembly>
  <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
  • SDK风格项目:在项目.csproj文件的<PropertyGroup>节点中添加如下配置,让VS自动生成绑定重定向:
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>

内容的提问来源于stack exchange,提问作者DoomerDGR8

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 14:54:05