Newtonsoft.Json常见问题处理

  1. Newtonsoft.Json 版本冲突问题

在 app.config 或者 web.config 中增加如下配置

问题描述

      我们最近在使用Redis作Session的集中化,中间碰到了一个如下问题:我们有一些项目比较老,引用了NewtonJson的4.0.3.0版本的DLL,但是Redis提供的C#集成DLL引用的是NewtonJson的7.0.0.0版本的DLL,但我们要在老项目中引用Redis集成DLL,因而就碰到了NewtonJson的版本冲突问题。

  

解决方案一

      我们可以通过配置web.config(或者app.config)来帮助我们解决这个问题。需要在web.config中配置如下节点:  

1 <runtime>
2     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
3       <dependentAssembly>
4         <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
5         <codeBase version="4.0.3.0" href="C:\v2.0\Newtonsoft.Json.dll" />
6         <codeBase version="7.0.0.0" href="C:\v3.5\Newtonsoft.Json.dll" />
7       </dependentAssembly>
8     </assemblyBinding>
9   </runtime>

    说明1:有的朋友博客上面说,需要在主项目中引用最新版本的DLL,经过实验是不需要这样做的。

    说明2:name填程序集的名称,pulicKeyToken是数字签名,version是程序集的版本,href指不同版本的程序集的地址,我尝试过,无法使用相对路径,必须使用绝对路径,可能使用相对路径也可以,但需要另外的配置,如果有朋友知道,麻烦分享一下。 

解决方案二

  如果在同一个项目中引用不同版本的DLL,但新版本的DLL兼容旧版本的DLL,那么可以采用如下的配置:

1 <runtime>
2     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
3       <dependentAssembly>
4         <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
5         <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
6       </dependentAssembly>
7     </assemblyBinding>
8   </runtime>

参考文档

   http://www.cnblogs.com/EugeneMay/p/4249709.html

  http://bbs.csdn.net/topics/390359027

一个项目引用不同版本dll

问题描述

一个项目引用不同版本的同一dll,会引发以下报错:

未能加载文件或程序集“xxx, Version=x.x.x.x, Culture=neutral, PublicKeyToken=xxxxxxxxxxxx”或它的某一个依赖项。系统找不到指定的文件 

这里来解决项目中同一dll的多版本问题。

解决方式

通过配置web.config配置文件(app.config或web.config)增加配置节点

不同场景有不同的解决方式,下面说明

1. 场景一   以高版本兼容

例如:新旧项目都引用Newtonsoft.Json,但是不同版本。需要以最高版本兼容。 

[html]  view plain  copy
  1. <runtime>  
  2.   <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">  
  3.     <dependentAssembly>  
  4.       <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30AD4FE6B2A6AEED" culture="neutral"/>  
  5.       <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>  
  6.     </dependentAssembly>  
  7.   </assemblyBinding>  
  8. </runtime>  

2. 场景二   同一dll两种版本共存

例如:项目自己引用log4net.dll 版本1.2.13.0 。添加第三方某个dll,第三方依赖log4net.dll版本1.2.9.0,项目中需要两种版本共存。
这里还分两种情况,dll的publicKeyToken相同还是不同 (publicKeyToken查询见说明1)
publicKeyToken相同,配置方法:
[html]  view plain  copy
  1. <runtime>  
  2.   <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">  
  3.     <dependentAssembly>  
  4.       <assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" />  
  5.       <codeBase version="1.2.13.0" href="bin\log4netdll\1_2_13\log4net.dll" />  
  6.       <codeBase version="1.2.9.0" href="bin\log4netdll\1_2_9\log4net.dll" />  
  7.     </dependentAssembly>  
  8.   </assemblyBinding>  
  9. </runtime>  

publicKeyToken不同,配置方法:
[html]  view plain  copy
  1. <runtime>  
  2.   <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">  
  3.     <dependentAssembly>  
  4.       <assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" />  
  5.       <codeBase version="1.2.13.0" href="bin\log4netdll\1_2_13\log4net.dll" />  
  6.     </dependentAssembly>  
  7.     <dependentAssembly>  
  8.       <assemblyIdentity name="log4net" publicKeyToken="b32731d11ce58905" />  
  9.       <codeBase version="1.2.9.0" href="bin\log4netdll\1_2_9\log4net.dll" />  
  10.     </dependentAssembly>  
  11.   </assemblyBinding>  
  12. </runtime>  

说明

1.publicKeyToken获取方式:使用vs的Tools Command Prompt命令行工具,输入:SN -T "path",例如:

  1. C:\Program Files (x86)\Microsoft Visual Studio 11.0>SN -T "D:\project\liberary\External\log4net.dll"    
  2.     
  3. Microsoft(R) .NET Framework 强名称实用工具 版本 4.0.30319.17929    
  4. 版权所有(C) Microsoft Corporation。保留所有权利。    
  5.     
  6. 公钥标记为 b32731d11ce58905   

注意:href的路径要保证正确。

参考文献

msdn对配置详解:  点击打开链接

参考文章

http://blog.csdn.NET/dang13579/article/details/72956684

  • 24
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值