我得到了错误

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>

它也没有起作用


当前回答

答案从2022年开始。 得到类似的异常错误:

Could not load file or assembly 'Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'

解决办法是进入网络。配置文件的项目,并进行以下编辑:

<dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"
        culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="11.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项目,请再次返回

我认为你指向错误的目标,把它改为4.5而不是6.0

<dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed"
        culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="4.5.0.0" />
</dependentAssembly>

这应该有用。

另一个潜在的问题是,如果元素在任何其他dependentAssembly元素上有不正确的配置,那么绑定重定向似乎就会无声地失败。

确保每个元素下只有一个元素。

在某些情况下,VS生成这个:

  <dependentAssembly>
    <assemblyIdentity ...
    <assemblyIdentity ...
  </dependentAssembly>

而不是

  <dependentAssembly>
    <assemblyIdentity ...
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity ...
  </dependentAssembly>

我花了很长时间才意识到这就是问题所在!

通常情况下,添加绑定重定向应该可以解决这个问题,但对我来说行不通。经过几个小时的思考,我意识到在我的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

另外,在使用NuGet还原的CI环境中,确保没有将部分文件夹检入源代码控制。默认情况下,在向源代码控制添加文件夹时,它将自动排除程序集。此外,这违背了在构建时恢复核包的整个目的。检查程序集或不检入任何包文件夹都将获得成功的构建,但更好的实践是不将包检入源代码控制。