如何排查.NET Core 2.1解决方案中导致构建失败的SDK依赖来源?
.NET Core 2.1 构建时 System.Runtime.CompilerServices.Unsafe 版本冲突问题解决
问题场景
- 维护未升级的.NET Core 2.1代码库,引入NuGet包FFMpegCore后,其依赖System.Text.Json 7.0.1
- 本地VS2017可正常构建,但构建服务器执行
dotnet build时,出现System.Runtime.CompilerServices.Unsafe与netcoreapp2.1不兼容的错误
排查过程
- 先将FFMpegCore降级至依赖System.Text.Json 6.0.1的版本,仍不支持.NET Core 2.1
- 再降级至依赖System.Text.Json 5.0.1的版本,该版本依赖的
System.Runtime.CompilerServices.Unsafe 5.0.0看似兼容,但构建服务器仍报错:
/home/build/.nuget/packages/system.runtime.compilerservices.unsafe/6.0.0/buildTransitive/netcoreapp2.0/System.Runtime.CompilerServices.Unsafe.targets(4,5): error : System.Runtime.CompilerServices.Unsafe doesn't support netcoreapp2.1. Consider updating your TargetFramework to netcoreapp3.1 or later. [/opt/build/develop/site/site.csproj]
- 本地Visual Studio显示FFMpegCore仅通过System.Text.Json依赖Unsafe 5.0.0,但构建服务器存在不兼容版本残留
- 尝试切换至依赖Newtonsoft.Json的旧版FFMpegCore,甚至切换到无FFMpegCore的分支,构建仍报错
解决方法
执行NuGet缓存清理命令:
dotnet nuget locals all --clear
清理完成后,构建服务器执行dotnet build恢复正常
结论
.NET Core 2.1环境下不应引入System.Runtime.CompilerServices.Unsafe包,若出现版本冲突残留问题,清理NuGet本地缓存是有效的解决手段
内容的提问来源于stack exchange,提问作者jlmt
相关产品推荐
相关产品推荐

