如何修复 json.net (Newtonsoft.Json) 运行时文件加载异常

本文关键字:运行时 文件 加载 异常 Json Newtonsoft 何修复 json net | 更新日期: 2023-09-27 18:34:48

每次运行项目时都会收到以下异常:

类型为"System.IO.FileLoadException"的异常发生在 mscorlib.dll但未在用户代码中处理 其他信息:无法加载文件或程序集"Newtonsoft.json,版本=4.5.0.0,区域性=中性,公钥令牌=30ad4fe6b2a6aeed"或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。(HRESULT的例外:0x80131040(

尝试了几乎所有我能在网上找到的解决方案。

我的软件包.config 文件:

<package id="Newtonsoft.Json" version="8.0.2" targetFramework="net451" />
and this in web.config file:
 <assemblyIdentity name="Newtonsoft.Json" PublicKeyToken="30ad4fe6b2a6aeed"  />
        <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="5.0.8"/>
      </dependentAssembly>

我什至使用以下命令来更新/重新安装 json.net

update-package Newtonsoft.Json -reinstall
update-package Newtonsoft.Json

我还能尝试什么?

如何修复 json.net (Newtonsoft.Json) 运行时文件加载异常

更改绑定重定向 newVersion 和 oldVersion 以匹配您尝试使用 Json.net 的版本:

  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" PublicKeyToken="30ad4fe6b2a6aeed"  />
    <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0"/>
  </dependentAssembly>

4.5 是框架版本,不是 Json.net 的版本

谢谢大家,我发现了问题。当我把它放在web.config中时,我没有添加culture="neutral"。

<dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" PublicKeyToken="30ad4fe6b2a6aeed"  />
    <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0"/>
  </dependentAssembly>

我遇到了这个问题,经过仔细检查,我意识到牛顿软件以某种方式被添加两次作为

<dependentAssembly>..</dependentAssembly>

在我的 web.config 文件中。 我有 8.0 版和 9.0 版条目。 我相信第二个条目是在我从 8.0 更新到 9.0 时创建的。 系统会自动创建一个新条目,但不会删除旧条目。

我遇到了同样的问题,我刚刚为我的项目安装了Nuget包'Newtonsoft.json'。它得到了解决