我得到了错误

fileloadexception:无法加载文件或程序集 “Newtonsoft。Json,版本=4.5.0.0,文化=中性, PublicKeyToken=30ad4fe6b2a6aeed'或其依赖项之一。的 定位程序集的清单定义与该程序集不匹配 参考。(异常来自HRESULT: 0x80131040)

用于我的CI构建

我尝试过的解决方案

<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>

它也没有起作用


当前回答

我花了几天时间试图解决这个令人沮丧的问题。我几乎试遍了网上能找到的所有方法。最后我发现这个错误可能是由一个解决方案中不同的目标. net项目版本(4.5和4.5.1)引起的(就像我的情况一样)。下面的步骤帮我解决了这个问题:

仔细检查解决方案中每个项目的. net版本。只需右键单击项目,然后进入属性。

If possible set the same .Net version for all projects. If not at least try to change the Startup project one (for me this was the one causing the issues). Remove all Newtonsoft.Json packs from the solution. uninstall-package newtonsoft.json -force Update all Newtonsoft.Json versions in all packages.config files, like so <package id="Newtonsoft.Json" version="7.0.1" targetFramework="net451" /> Reinstall Newtonsoft.Json from "Package Manager Console" with: install-package newtonsoft.json Rebuild the solution

(可选)7。如果更改了Startup项目,请再次返回

其他回答

在包管理器控制台执行:Update-Package -重装Newtonsoft.Json。

更新

我最初是作为评论发布这篇文章的,但正如@OwenBlacker建议的那样,我就把它放在这里:

如果在这样做之后仍然得到错误,那么最终对我有用的是我删除了Json。Net的<dependentAssembly>节从我的.config文件。重新安装会使它回来,如果它不存在,显然你需要删除它。在包本身有一个正常的解决方案之前,恐怕这个手动步骤是必须的。

注意:在这样做之前,请阅读下面的评论。

根据René下面的评论,请注意,答案中发布的命令将在解决方案中的每个项目中重新安装软件包。如果你用牛顿软体。Json包在几个项目中,可能使用不同的版本,只是执行上面的命令可能会有不想要的后果。

通常情况下,添加绑定重定向应该可以解决这个问题,但对我来说行不通。经过几个小时的思考,我意识到在我的web.config中有一个xmlns属性导致了问题。从Web中的配置节点中删除xmlns属性后。配置时,绑定重定向按预期工作。

http://www.davepaquette.com/archive/2014/10/02/could-not-load-file-or-assembly-newtonsoft-json-version4-5-0-0.aspx

我写了一个程序来自动修复这个问题。它将确保您的程序能够使用硬盘驱动器上目标程序集的最新版本,而不仅仅是依赖库需要的版本。

https://github.com/BackTrak/DependencyFixup/releases/tag/1.0.0.0

无需再猜测,它将自动为您修复所有清单引用问题。

如果您在同一解决方案中使用多个项目 和另一个的图书馆 检查是否所有项目都有相同版本的Newtonsoft。Json

下面的部分添加到您的web.config

<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>