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

.NET Framework 4.8类库第三方软件依赖加载错误求助

解决.NET Framework 4.8类库第三方软件加载依赖问题

问题核心

你开发的.NET Framework 4.8类库依赖System.Text.Json,第三方软件加载时出现程序集缺失,本质是依赖项未被正确部署,以及传递依赖的版本冲突导致程序集绑定失败。

解决方案

1. 强制复制所有依赖到输出目录

  • 右键项目中System.Text.Json和System.Runtime.CompilerServices.Unsafe的引用,将复制本地属性设为True,确保Release目录包含所有必要的依赖DLL。
  • 若可视化设置不生效,直接修改项目文件(.csproj):
    <ItemGroup>
      <PackageReference Include="System.Text.Json" Version="6.0.0">
        <PrivateAssets>all</PrivateAssets>
        <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      </PackageReference>
      <PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0">
        <PrivateAssets>all</PrivateAssets>
        <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      </PackageReference>
    </ItemGroup>
    

2. 添加程序集绑定重定向

在第三方软件的配置文件(如xxx.exe.config)中添加版本重定向,将旧版本的依赖指向你使用的6.0.0版本:

<configuration>
  <runtime>
    <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-6.0.0.0" 
                         newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Text.Json" 
                          publicKeyToken="cc7b13ffcd2ddd51" 
                          culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" 
                         newVersion="6.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

如果第三方软件不允许修改其配置,可使用ILMerge或Costura.Fody工具将所有依赖合并到你的类库DLL中,消除外部依赖。

3. 验证依赖完整性

  • 用dotnet list package --include-transitive命令查看类库的所有传递依赖,确认无版本冲突。
  • 用ILSpy打开你的类库DLL,检查引用的System.Runtime.CompilerServices.Unsafe版本是否为6.0.0,且对应的DLL存在于Release目录。

4. 替代方案:切换到Newtonsoft.Json

若System.Text.Json的依赖问题难以解决,可改用Newtonsoft.Json(Json.NET)——它在.NET Framework 4.8中兼容性更强,依赖链更简单,能避免此类加载错误。

内容的提问来源于stack exchange,提问作者A. CLAUDE

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 21:50:37