未能加载文件或程序集Newtonsoft.Json,版本不一致问题

1. 问题描述

A程序集引用了 Newtonsoft.Json 6.0程序集
B程序集引用了 Newtonsoft.Json 10.0程序集
此时A引用B,就会报:发现同一依赖程序集的不同版本间存在无法解决的冲突 这一警告,执行程序就会报错–System.IO.FileNotFoundException: 未能加载文件或程序集Newtonsoft.Json xx.x的错误

 A:引用Newtonsoft.Json 6.0
        FuncA()
        {
            var obj= Newtonsoft.Json.Obj;
            B.FuncB();
        }

B: 引用Newtonsoft.Json 10.0
FuncB()
{
return Newtonsoft.Json.Obj;
}

2. 原因分析

  1. 这是因为依赖顺序引起的。A引用了B,首先会先生成B,而B引用了 Newtonsoft.Json 10.0,那么VS就会将源引用文件(Newtonsoft.Json 10.0)复制到B程序集同一目录(bin/Debug)下,名为Newtonsoft.Json.dll文件,其内嵌程序集版本为10.0。
  2. 然后A引用了B,所以会将B程序集和B程序集的依赖项10.0(Newtonsoft.Json.dll)给复制到A的程序集目录下,而A又引用了Newtonsoft.Json 6.0程序集文件,所以又将6.0的dll文件给复制到自己程序集目录下。因为两个Newtonsoft.Json.dll重名,所以直接覆盖了前者,那么只保留了Newtonsoft.Json 6.0。
  3. 当我们调用Func方法中的B.FuncB()时候,CLR会搜索B程序集,找到后再调用 return Newtonsoft.Json.Obj 这行代码,而这行代码又用到了Newtonsoft.Json程序集,接下来CLR搜索Newtonsoft.Json.dll,文件名称满足,接下来CLR判断其标识,发现版本号是6.0,与B程序集清单里注册的10.0版本不符,故而才会报出异常:未能加载文件或程序集Newtonsoft.Json 10.0。

3 解决方案

方案1. 通过bindingRedirect节点重定向,即当找到10.0的版本时,给定向到6.0版本

<runtime>
     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <dependentAssembly>
             <assemblyIdentity name="Newtonsoft.Json"
                               publicKeyToken="30ad4fe6b2a6aeed"
                               culture="neutral" />
             <bindingRedirect oldVersion="10.0.0.0"
                              newVersion="6.0.0.0" />
         </dependentAssembly>
     </assemblyBinding>
</runtime>

方案2. 对每个版本指定codeBase路径,然后分别放上不同版本的程序集,这样就可以加载两个相同的程序集

<runtime>
	 <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
	      <dependentAssembly>
	          <assemblyIdentity name="Newtonsoft.Json"
	                            publicKeyToken="30ad4fe6b2a6aeed"
	                            culture="neutral" />
	          <codeBase version="6.0.0.0"
	                    href="E:\6.0\Newtonsoft.Json.dll" />
	      </dependentAssembly>
	      <dependentAssembly>
	          <assemblyIdentity name="Newtonsoft.Json"
	                            publicKeyToken="30ad4fe6b2a6aeed"
	                            culture="neutral" />
	          <codeBase version="10.0.0.0"
	                    href="E:\10.0\Newtonsoft.Json.dll" />
	      </dependentAssembly>
	  </assemblyBinding>
</runtime>

 
 
END
转载自【神韵凌天 】
文章链接【https://blog.csdn.net/qq_31176861/article/details/84772964】
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
未能加载文件程序Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed”或它的某一个依赖项的问题是由于程序清单定义与程序引用不匹配引起的。 这意味着在代码中引用了一个不匹配的Newtonsoft.Json程序。 解决这个问题的方法是删除web.config文件中指定版本的配置。 在webconfig中找到类似于以下内容的配置代码,并删除它: <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> 删除这段代码后,重新编译和运行应用程序,应该就能解决这个问题了。 另外,你可以参考链接https://www.cnblogs.com/BungeeJumping/p/5486624.html,它提供了一些解决这个问题的备忘录。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Newtonsoft.Json for .net 2.0](https://download.csdn.net/download/me5572/9672989)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [未能加载文件程序Newtonsoft.Json”或它的某一个依赖项。找到的程序清单定义与程序引用不匹配。 ...](https://blog.csdn.net/weixin_33672400/article/details/85955960)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [未能加载文件程序Newtonsoft.Json解决方法](https://blog.csdn.net/qq_38472451/article/details/119867133)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值