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

Protobuf-net 3.2.30依赖多版本Unsafe.dll致运行异常,如何修复部署?

解决System.Runtime.CompilerServices.Unsafe版本冲突导致的FileLoadException问题

针对你遇到的protobuf-net 3.2.30依赖的System.Runtime.CompilerServices.Unsafe版本冲突、运行时找不到对应版本的问题,可通过以下几种方案解决:

方案1:添加程序集绑定重定向(适用于.NET Framework)

在项目的app.config或web.config中添加绑定重定向配置,将所有版本的System.Runtime.CompilerServices.Unsafe统一指向发布目录中存在的6.0.0版本:

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" 
                          publicKeyToken="b03f5f7f11d50a3a" 
                          culture="neutral" />
        <!-- 将所有旧版本(包括4.0.4.1、4.5.3)重定向到6.0.0版本 -->
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

该配置会让CLR在加载任何旧版本的该程序集时,自动改用6.0.0版本(该版本向后兼容低版本API)。

方案2:对齐依赖包的版本

调整依赖包版本,让所有依赖项指向同一个版本的System.Runtime.CompilerServices.Unsafe:

  • 升级System.Memory:将System.Memory从4.5.5升级到5.0.0及以上版本,这类版本依赖的System.Runtime.CompilerServices.Unsafe版本更高,NuGet会自动解析为6.0.0版本(与System.Collections.Immutable 7.0.0的依赖对齐)。
  • 降级System.Collections.Immutable:将System.Collections.Immutable从7.0.0降级到6.0.0版本,该版本依赖的是System.Runtime.CompilerServices.Unsafe 4.5.3,与System.Memory 4.5.5的依赖版本一致,发布后只会输出4.5.3版本的DLL。

方案3:优化.NET Core/.NET 5+项目的依赖处理(若使用该框架)

对于.NET Core及以上版本,默认会自动处理绑定重定向,但如果仍有问题,可在项目文件(.csproj)中添加以下配置,强制生成绑定重定向:

<PropertyGroup>
  <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
  <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
</PropertyGroup>

同时,确保项目使用PackageReference方式管理NuGet依赖(而非packages.config),这能让NuGet更精准地解析依赖版本。

方案4:强制指定依赖版本

在项目文件中直接指定System.Runtime.CompilerServices.Unsafe的版本为6.0.0,覆盖所有依赖项的版本要求:

<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />

NuGet会强制所有依赖项使用这个版本,避免版本分裂。

内容的提问来源于stack exchange,提问作者soumya sambit Kunda

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 16:41:08