多版本Newtonsoft.Json DLL引用冲突问题求助
原本程序引用13.0.0.0版本的Newtonsoft.Json运行正常,引入第三方DLL后出现以下错误:
System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
已尝试以下两种方案但未解决:
1. 配置文件修改
第一种配置:
<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30AD4FE6B2A6AEED" culture="neutral"/> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/> </dependentAssembly> </assemblyBinding> </runtime>
第二种配置:
<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" /> <bindingRedirect version="4.0.0.0" href="D:\WebService\TaskWebService\bin\Net40\Newtonsoft.Json40.dll" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" /> <bindingRedirect version="13.0.0.0" href="D:\WebService\TaskWebService\bin\Newtonsoft.Json.dll" /> </dependentAssembly> </assemblyBinding> </runtime>
2. 别名引用
extern alias NewJson40; using NewJson40NameSpace = NewJson40::Newtonsoft.Json; using NewJson40LinqNameSpace = NewJson40::Newtonsoft.Json.Linq; ......
可行解决办法
1. 修正绑定重定向配置
错误提示里第三方依赖的是PublicKeyToken=null的Newtonsoft.Json 4.0.0.0,说明该组件引用的是未签名版本,而你当前用的是官方签名版本(PublicKeyToken=30AD4FE6B2A6AEED),之前的重定向没覆盖无签名的情况。修改配置文件:
<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <!-- 处理官方签名版本的重定向 --> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30AD4FE6B2A6AEED" culture="neutral"/> <bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0"/> </dependentAssembly> <!-- 处理无签名版本的重定向,映射到现有13.0.0.0版本 --> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="null" culture="neutral"/> <bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0"/> </dependentAssembly> </assemblyBinding> </runtime>
2. 检查第三方组件依赖细节
用ILSpy、dotPeek等工具反编译第三方DLL,确认它依赖的Newtonsoft.Json是否确实是无签名版本。如果第三方组件是自定义编译的,可联系提供者更换为依赖签名版本的包。
3. 手动指定版本路径(万不得已时用)
如果必须保留两个版本,把无签名的4.0.0.0版本Newtonsoft.Json.dll复制到程序输出目录,再修改配置:
<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="null" culture="neutral" /> <codeBase version="4.0.0.0" href="Newtonsoft.Json.4.0.0.dll" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30AD4FE6B2A6AEED" culture="neutral" /> <codeBase version="13.0.0.0" href="Newtonsoft.Json.dll" /> </dependentAssembly> </assemblyBinding> </runtime>
注意:这种方式可能引发类型兼容性问题,谨慎使用。
4. NuGet统一管理版本
卸载当前的Newtonsoft.Json,通过NuGet重新安装13.0.0.0版本,NuGet会自动生成正确的绑定重定向。同时检查第三方组件是否可通过NuGet安装,确保所有依赖的版本统一。
内容的提问来源于stack exchange,提问作者Karos

