C# 无法加载文件或程序集 Newtonsoft.json.dll

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9426723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:22:39  来源:igfitidea点击:

Could not load file or assembly Newtonsoft.json.dll

c#.netasp.net-mvcwinforms

提问by Sagar

“Could not load file or assembly 'Newtonsoft.Json, Version=4.0.3.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)”.

“无法加载文件或程序集 'Newtonsoft.Json, Version=4.0.3.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' 或其依赖项之一。定位的程序集的清单定义与程序集引用不匹配。(来自 HRESULT 的异常:0x80131040)”。

'NewtonsoftJson.dll' assembly is required for posting to twitter .The version used is 4.0.3.0.

发布到 twitter 需要“NewtonsoftJson.dll”程序集。使用的版本是 4.0.3.0。

And the same assembly(but diff version 4.0.0.0) is used as the dependent assembly by facebook C# api(dll). However the above assembly(4.0.3.0) does not work for both the cases (ie for posting to twitter and for login to facebook). In order to solve it ,created a separate project for posting to twitter and reference the assembly(4.0.3.0) separately (in the project for twitter post).And another version(4.0.0.0) was added as reference in main project for facebook but still the error comes.If twitter project is disabled and ran then the facebook login works fine and vice vera.

并且相同的程序集(但差异版本 4.0.0.0)被 facebook C# api(dll) 用作依赖程序集。然而,上面的程序集(4.0.3.0)不适用于这两种情况(即发布到 twitter 和登录到 facebook)。为了解决这个问题,创建了一个单独的项目来发布到 twitter 并单独引用程序集(4.0.3.0)(在 twitter post 的项目中)。另一个版本(4.0.0.0)被添加到 facebook 的主项目中作为参考但仍然出现错误。如果 twitter 项目被禁用并运行,则 facebook 登录工作正常,反之亦然。

I have done lot of research and tried the following.

我做了很多研究并尝试了以下方法。

delete temporary asp.net files clean solution restart computer

删除临时asp.net文件清理解决办法重启电脑

Even tried to uninstall the assembly from gac(however its not registered there).

甚至试图从 gac 卸载程序集(但它没有在那里注册)。

Please help me on this as its not working. Thanks, S

请帮助我解决这个问题,因为它不起作用。谢谢,S

采纳答案by Mike Zboray

It's not clear what the relationships of the projects are, or when this error is occuring, but here's a guess.

目前尚不清楚项目的关系是什么,或者何时发生此错误,但这是一个猜测。

You have 3 projects the facebook project (refs 4.0.0.0 version), twitter project (refs 4.0.3.0 version), and a main project that refs both of those projects. You may be able to build this solution, but when you run the assembly binding will fail. Why? because the default behavior is to copy assemblies locally before running. What happens is that the first project to build copies to bin (say twitter) then the second project builds (facebook), then main. However, at the end of this the 4.0.0.0 version is sitting bin folder. When you run, as soon as you invoke something from twitter that tries to use the problem assembly the bind fails because it longer has access to the 4.0.3.0 version of the assembly.

您有 3 个项目,facebook 项目(refs 4.0.0.0 版本)、twitter 项目(refs 4.0.3.0 版本)和一个引用这两个项目的主项目。您也许能够构建此解决方案,但是当您运行程序集绑定时将失败。为什么?因为默认行为是在运行之前在本地复制程序集。发生的情况是第一个项目构建副本到 bin(比如 twitter),然后第二个项目构建(facebook),然后是 main。但是,在此结束时,4.0.0.0 版本位于 bin 文件夹中。当您运行时,只要您从 twitter 调用一些尝试使用问题程序集的内容,绑定就会失败,因为它不再可以访问 4.0.3.0 版本的程序集。

There are a couple of ways around this. One is register both assemblies in the GAC. If that isn't doable then look into assembly binding redirectionin your config file. Another is to register for the AssemblyResolve eventand load the assembly programmatically.

有几种方法可以解决这个问题。一种是在 GAC 中注册这两个程序集。如果这不可行,请查看配置文件中的程序集绑定重定向。另一种方法是注册AssemblyResolve 事件并以编程方式加载程序集。

回答by Ayana

Add the following to your app.config file:

将以下内容添加到您的 app.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-4.5.0.0" newVersion="4.5.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

within your <configuration></configuration>tags

在你的<configuration></configuration>标签内

回答by André Monteiro

Install package Newtonsoft in all projects.

在所有项目中安装包 Newtonsoft。

Install-Package Newtonsoft.Json -Version 8.0.3