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

求助:.NET项目集成时程序集绑定重定向及版本冲突问题

问题描述

我有一个基于GodSharp.Opc.Ua.Client的项目,该NuGet包依赖OPCFoundation.Netstandart.Opc.Ua.Core,后者又依赖Newtonsoft.Json v13.0.3。我的项目需要以DLL形式集成到一个依赖Newtonsoft.Json v12.0.3且无法升级的大型项目中。

我尝试用绑定重定向解决版本冲突,但调用Opc.Ua.Core相关代码时,抛出了Newtonsoft.Json的FileNotFoundException。按提示重新生成绑定重定向后,该问题暂时解决,但将用户控件集成到其他项目时,首次使用GodSharp.Opc.Ua.Client元素会触发System.Memory和System.Runtime.CompilerServices.Unsafe的FileLoadException——但在简单测试窗口中使用该控件却无此问题,甚至移除相关NuGet包后错误依然存在。

相关异常信息

Newtonsoft.Json 文件未找到异常

+       e   {"未能找到文件或程序集\"Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed\"或它的某一个依赖项。系统找不到指定的文件。":"Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed"} System.Exception {System.IO.FileNotFoundException}

System.Memory 和 System.Runtime.CompilerServices.Unsafe 加载异常

System.IO.FileLoadException: "未能找到文件或程序集\"System.Memory, 
Version=4.0.1.1, Culture=neutral, 
PublicKeyToken=cc7b13ffcd2ddd51\"或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)"

System.IO.FileLoadException: "未能找到文件或程序集\"System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, 
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a\"或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)"

当前绑定重定向配置

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
    </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.1.2" newVersion="4.0.1.2" />
      </dependentAssembly>
      <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="Opc.Ua.Client" publicKeyToken="bfa7a73c5cf4b6e8" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.372.0" newVersion="1.5.372.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Opc.Ua.Configuration" publicKeyToken="bfa7a73c5cf4b6e8" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.372.0" newVersion="1.5.372.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Opc.Ua.Security.Certificates" publicKeyToken="bfa7a73c5cf4b6e8" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.373.0" newVersion="1.5.373.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-13.0.3.0" newVersion="12.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Opc.Ua.Core" publicKeyToken="bfa7a73c5cf4b6e8" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.4.372.0" newVersion="1.4.372.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Extensions.Logging.Abstractions" publicKeyToken="adb9793829ddae60" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.4" newVersion="6.0.0.4" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

解决方案

1. 修正绑定重定向版本范围

当前System.Memory和System.Runtime.CompilerServices.Unsafe的重定向未覆盖异常中提到的版本(4.0.1.1、4.0.4.1),需调整规则:

  • System.Memory的重定向范围已包含4.0.1.1,但需确认宿主项目中实际存在4.0.1.2版本的DLL
  • System.Runtime.CompilerServices.Unsafe需将newVersion改为异常中请求的4.0.4.1(或宿主项目已有的兼容版本)

修改后的重定向示例:

<dependentAssembly>
  <assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-4.0.1.2" newVersion="4.0.1.2" />
</dependentAssembly>
<dependentAssembly>
  <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="4.0.4.1" />
</dependentAssembly>

2. 将绑定重定向配置到宿主项目

类库项目的绑定重定向不会自动生效,必须把所有相关重定向规则复制到大型宿主项目的app.config或web.config中,才能让CLR正确解析依赖。

3. 清理并校验依赖

  • 删除解决方案的packages文件夹,重新还原NuGet依赖
  • 检查宿主项目中是否已有冲突包的重定向规则,避免重复或冲突
  • 确认你的DLL输出目录包含正确版本的依赖DLL,或宿主项目能通过NuGet找到这些依赖

4. 强制指定依赖版本(可选)

在你的项目.csproj文件中添加强制版本声明,确保编译时使用指定版本:

<PackageReference Include="System.Memory" Version="4.0.1.2" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.0.4.1" />

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 13:35:54