无法加载文件或程序集 'Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' 或其依赖项之一
Could not load file or assembly 'Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies
这个错误让我抓狂。
我在名为 BaseServices 的 class 库之一中安装了 SendGrid NuGet 包,它依赖于 Newtonsoft.Json v7.0.1,所以它将它安装在我的包文件夹中并引用它。
在 class 库中,我有这个绑定重定向:
<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>
在 ASP.NET MVC 应用程序的 web.config 中,即使用我的 class 库的客户端应用程序,我有一个针对低于 v6 的版本的程序集绑定重定向指向Newtonsoft.Json 库的 v6 像这样:
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral"
publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
当我 运行 我的电子邮件发送代码位于 BaseServices 库中时,我收到此错误:
The thread 0x1a4c has exited with code 0 (0x0).
System.IO.FileLoadException: Could not load file or assembly
'Newtonsoft.Json, Version=7.0.0.0, Culture=neutral,
PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The
located assembly's manifest definition does not match the assembly
reference. (Exception from HRESULT: 0x80131040) File name:
'Newtonsoft.Json, Version=7.0.0.0, Culture=neutral,
PublicKeyToken=30ad4fe6b2a6aeed' at
SendGrid.Helpers.Mail.Mail.Get() at
BaseServices.EmailService.SendAsync(EmailMessage message) in
MyFolder\BaseServices\EmailService.cs:line 39
=== Pre-bind state information === LOG: DisplayName = Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
(Fully-specified) LOG: Appbase = file:///MyFolder/Web/ LOG: Initial
PrivatePath = MyFolder\Web\bin Calling assembly : SendGrid,
Version=7.0.3.0, Culture=neutral, PublicKeyToken=4f047e93159395ca.
=== LOG: This bind starts in default load context. LOG: Using application configuration file: MyFolder\Web\web.config LOG: Using
host configuration file:
C:\Users\computer\Documents\IISExpress\config\aspnet.config LOG: Using
machine configuration file from
C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Newtonsoft.Json, Version=7.0.0.0,
Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed LOG: Attempting
download of new URL
file:///C:/Users/computer/AppData/Local/Temp/Temporary ASP.NET
Files/root/ef9cacdf/e639667a/Newtonsoft.Json.DLL. LOG: Attempting
download of new URL
file:///C:/Users/computer/AppData/Local/Temp/Temporary ASP.NET
Files/root/ef9cacdf/e639667a/Newtonsoft.Json/Newtonsoft.Json.DLL. LOG:
Attempting download of new URL
file:///MyFolder/Web/bin/Newtonsoft.Json.DLL. WRN: Comparing the
assembly name resulted in the mismatch: Major Version ERR: Failed to
complete setup of assembly (hr = 0x80131040). Probing terminated.
您的解决方案中有 2 个不同版本的 JSON.NET 库。要解决此问题,您应该将它们升级到最新版本。请按照以下步骤操作:
- 打开解决方案资源管理器
- 右键单击解决方案名称
- Select 管理解决方案的 Nuget 包
- Select 从菜单更新
- 更新JSON.NET包
这将解决您的问题。
这种疯狂的事情在我身上发生过很多次。您需要确保 Newtonsoft.Json 程序集版本在这些地方是一致的:
- 项目参考(dll 版本)
- Web.config
- Packages.config
我遇到了同样的异常消息并发现了这个线程。希望以下内容可以帮助其他人。
我的解决方案包含多个项目。其中一个项目使用了 Microsoft.AspNet.WebApi.Client。它的依赖项之一是 Newtsonsoft.Json 库。
但是,Microsoft.AspNet.WebApi.Client 使用的库版本是 6.0 版。
接下来的问题是,在某些时候,开发人员会收到更新通知。所以我们更新 JSON 库。一切都很好,对吧?
然后我们添加一个创建 .msi 安装程序的安装项目。啊……在这个 .msi 中,我们现在看到 JSON 库的两个版本。嗯?那是怎么发生的。当然,我们删除了旧的。
然后,在某些时候,在测试期间,我们收到有关版本 6.0 的异常消息。但是,现在,当我们浏览 .config 文件等时,我们看到了对版本 11.0+ 的引用。
我在此处看到了各种线程的各种修复。有些人谈论强制卸载库和删除这些 .config 文件中的 XML 引用。
对我有用的是卸载 Microsoft.AspNet.WebApi.Client 和 JSON 库并重新安装 Microsoft.AspNet.WebApi.Client。后者带回了其适当的依赖性。
教训:如果您在安装项目中看到一个 .dll 的多个版本,请确保您知道需要删除哪个版本。
不过,令人困惑的是,在升级库后,在调试模式下,解决方案继续 运行,没有任何意外。只有当我去测试部署时,我才 运行 进入这个问题。
此外,第一个答案所建议的升级对我来说不是解决方案。
这个错误让我抓狂。
我在名为 BaseServices 的 class 库之一中安装了 SendGrid NuGet 包,它依赖于 Newtonsoft.Json v7.0.1,所以它将它安装在我的包文件夹中并引用它。
在 class 库中,我有这个绑定重定向:
<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>
在 ASP.NET MVC 应用程序的 web.config 中,即使用我的 class 库的客户端应用程序,我有一个针对低于 v6 的版本的程序集绑定重定向指向Newtonsoft.Json 库的 v6 像这样:
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral"
publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
当我 运行 我的电子邮件发送代码位于 BaseServices 库中时,我收到此错误:
The thread 0x1a4c has exited with code 0 (0x0). System.IO.FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) File name: 'Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' at SendGrid.Helpers.Mail.Mail.Get() at BaseServices.EmailService.SendAsync(EmailMessage message) in MyFolder\BaseServices\EmailService.cs:line 39
=== Pre-bind state information === LOG: DisplayName = Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed (Fully-specified) LOG: Appbase = file:///MyFolder/Web/ LOG: Initial PrivatePath = MyFolder\Web\bin Calling assembly : SendGrid, Version=7.0.3.0, Culture=neutral, PublicKeyToken=4f047e93159395ca. === LOG: This bind starts in default load context. LOG: Using application configuration file: MyFolder\Web\web.config LOG: Using host configuration file: C:\Users\computer\Documents\IISExpress\config\aspnet.config LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config. LOG: Post-policy reference: Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed LOG: Attempting download of new URL file:///C:/Users/computer/AppData/Local/Temp/Temporary ASP.NET Files/root/ef9cacdf/e639667a/Newtonsoft.Json.DLL. LOG: Attempting download of new URL file:///C:/Users/computer/AppData/Local/Temp/Temporary ASP.NET Files/root/ef9cacdf/e639667a/Newtonsoft.Json/Newtonsoft.Json.DLL. LOG: Attempting download of new URL file:///MyFolder/Web/bin/Newtonsoft.Json.DLL. WRN: Comparing the assembly name resulted in the mismatch: Major Version ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.
您的解决方案中有 2 个不同版本的 JSON.NET 库。要解决此问题,您应该将它们升级到最新版本。请按照以下步骤操作:
- 打开解决方案资源管理器
- 右键单击解决方案名称
- Select 管理解决方案的 Nuget 包
- Select 从菜单更新
- 更新JSON.NET包
这将解决您的问题。
这种疯狂的事情在我身上发生过很多次。您需要确保 Newtonsoft.Json 程序集版本在这些地方是一致的:
- 项目参考(dll 版本)
- Web.config
- Packages.config
我遇到了同样的异常消息并发现了这个线程。希望以下内容可以帮助其他人。
我的解决方案包含多个项目。其中一个项目使用了 Microsoft.AspNet.WebApi.Client。它的依赖项之一是 Newtsonsoft.Json 库。
但是,Microsoft.AspNet.WebApi.Client 使用的库版本是 6.0 版。
接下来的问题是,在某些时候,开发人员会收到更新通知。所以我们更新 JSON 库。一切都很好,对吧?
然后我们添加一个创建 .msi 安装程序的安装项目。啊……在这个 .msi 中,我们现在看到 JSON 库的两个版本。嗯?那是怎么发生的。当然,我们删除了旧的。
然后,在某些时候,在测试期间,我们收到有关版本 6.0 的异常消息。但是,现在,当我们浏览 .config 文件等时,我们看到了对版本 11.0+ 的引用。
我在此处看到了各种线程的各种修复。有些人谈论强制卸载库和删除这些 .config 文件中的 XML 引用。
对我有用的是卸载 Microsoft.AspNet.WebApi.Client 和 JSON 库并重新安装 Microsoft.AspNet.WebApi.Client。后者带回了其适当的依赖性。
教训:如果您在安装项目中看到一个 .dll 的多个版本,请确保您知道需要删除哪个版本。
不过,令人困惑的是,在升级库后,在调试模式下,解决方案继续 运行,没有任何意外。只有当我去测试部署时,我才 运行 进入这个问题。
此外,第一个答案所建议的升级对我来说不是解决方案。