无法加载文件或程序集“Newtonsoft.Json”或其某个依赖项

11

首先,这不仅仅是重复的问题。以下问题的答案都对我没有用。

http://goo.gl/tS40cn
http://goo.gl/pH6v2T

我刚使用Nuget包管理器更新了所有的软件包,然后开始收到这个错误信息。 无法加载文件或程序集“Newtonsoft.Json,Version=6.0.0.0,Culture=neutral,PublicKeyToken=30ad4fe6b2a6aeed”或其任何依赖项。找到的程序集清单定义与程序集引用不匹配。(来自 HRESULT: 0x80131040 的异常) 我的软件包配置文件为:
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net45" />

Web.config 文件包含以下代码:

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

来自 Newtonsoft.Json 的参考属性

enter image description here

根据类似问题的答案,我尝试了以下方法:
  • 使用Update-Package –reinstall Newtonsoft.Json重新安装软件包
  • Web.config中删除Newtonsoft.JsondependentAssembly配置
  • 更改dependentAssembly中的newVersion6.0.0.07.0.0.0。但这样会导致新错误。
  • 也尝试过Get-Project -All | Add-BindingRedirect,它将Newtonsoft.JsonnewVersion更改为4.5.0.0。但问题仍未解决。
请帮我解决这个问题。

3
在构建之前,你清理了构建文件夹的内容吗?你的目标框架版本与引用的dll版本相同吗? - ilansch
10个回答

15

我知道这已经很旧了,但我刚刚遇到了同样的问题。我的问题是解决方案中有多个项目使用了Newtonsoft.Json,但是某些项目使用的版本不同。我将它们全部更新到最新版本(当我输入时为9.0.1),问题就解决了。

无论如何...如果还有人在处理此问题,请确保在解决方案中的每个项目中更新该软件包。

希望对你有所帮助。


那真的很不直观,但它起作用了。我没想到解决方案中不同项目中的引用会互相影响。好东西! - Niklas
@Niklas,是啊,错误信息真的没有任何帮助。很高兴你解决了问题! - Casey Crookston

2
在我的MVC项目中添加Newtonsoft参考解决了我的问题。

2

在尝试以上方法(以及其他帖子)后,我使用软件包管理器从受影响的项目中卸载了以下所有内容:

Microsoft.AspNet.WebApi
Microsoft.AspNet.Client
Microsoft.AspNet.Core
Microsoft.AspNet.WebHost
Newtonsoft.Json

然后重新安装了Microsoft.AspNet.WebApi,它自动安装了.Client、.Core、.WebHost、.Json。


1
运行 Update-Package Newtonsoft.Json -Reinstall 它将删除对4.5版本的引用,并重新安装在package.config中引用的更新版本。同时还会更新绑定重定向,应如下所示:
<dependentAssembly>
  <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
  <bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />
</dependentAssembly>

既然你在问题中说过已经尝试过这个方法,你可能需要首先手动删除现有的引用。你还需要确保文件在磁盘上不是只读的,或者被源代码控制锁定。


谢谢!我记得我已经尝试过了。但是我会再试一次并尽快在这里更新。 - shashwat
抱歉回复晚了。按照您的建议运行命令后,我仍然遇到相同的错误。它将绑定重定向设置为<bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.5.0.0" />。我尝试更改为您提供的版本<bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="7.0.0.0" />。但是它生成了这个错误Could not load file or assembly 'Newtonsoft.Json' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) - shashwat

1

我自己也遇到了这个错误,首先使用了Update-Package –reinstall Newtonsoft.Json -IncludePrerelease,但没有成功,然后使用了Install-Package Newtonsoft.Json。它起作用了。


0
  1. 在您的VS解决方案资源管理器中,删除Newtonsoft.Json引用。
  2. 这里下载6.0二进制文件。
  3. 解压缩文件。
  4. 手动添加Newtonsoft库。从Visual Studio中,右键单击“引用”并选择“添加引用”。
  5. 点击浏览
  6. 导航到Net45下的提取文件夹,并选择Newtonsoft.Json.dll
  7. 如果不起作用,请再次执行整个过程并尝试使用Net40。

0
请将配置更改为如下所示:

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

0
在我的情况下,以下代码存在于我本地调试版本的解决方案中,但不在我的生产服务器代码版本中。将该代码添加到我的服务器Web.config文件中解决了问题。
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" />
    <bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
  </dependentAssembly>
</assemblyBinding>


0
在包管理器控制台中运行此命令:
PM> Install-Package Newtonsoft.Json -Version 6.0.1

安装失败。出现错误 Install-Package : 更新 'Newtonsoft.Json 7.0.1' 到 'Newtonsoft.Json 6.0.1' 失败。无法找到与 'Newtonsoft.Json 6.0.1' 兼容的 'Microsoft.AspNet.WebApi.Client' 版本 - shashwat

0

检查项目引用中的“Newtonsoft.Json”版本。将该版本添加到Web配置文件中。它会起作用。 例如: 您的Web配置文件如下:

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

如果您在“参考文献”中的版本为“9.0.0.0”,请更改为以下内容:
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json"publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-7.0.0.0" newVersion="9.0.0.0"/>
</dependentAssembly>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接