简体   繁体   English

无法加载文件或程序集 'Newtonsoft.Json,版本 = 10.0.0.0

[英]Could not load file or assembly 'Newtonsoft.Json, Version=10.0.0.0

I do not understand the following problem I am facing.我不明白我面临的以下问题。

I have 3 projects, A, B and C. All of them have a reference to Newtonsoft.Json , version 12.0.1我有 3 个项目,A、B 和 C。它们都引用了Newtonsoft.Json ,版本12.0.1

Project a has a reference to both projects B and C. Project B, has a references to Microsoft.IdentityModel.Tokens.Jwt项目 A 引用了项目 B 和 C。项目 B 引用了Microsoft.IdentityModel.Tokens.Jwt

When I run project A, I get the following runtime error when trying to execute the function necessary for creating my token.当我运行项目 A 时,在尝试执行创建令牌所需的函数时出现以下运行时错误。

Message:信息:

"The type initializer for 'System.IdentityModel.Tokens.Jwt.JsonExtensions' threw an exception." “'System.IdentityModel.Tokens.Jwt.JsonExtensions' 的类型初始值设定项引发异常。”

Inner Exception:内部异常:

"Could not load file or assembly 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified." “无法加载文件或程序集 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' 或其依赖项之一。系统找不到指定的文件。”

I have verified that all projects reference the same Newtonsoft.Json package with version 12.0.1 .我已经验证所有项目都引用了相同的Newtonsoft.Json包,版本为12.0.1 I have also checked my project files, to make sure there is no reference to Newtonsoft.Json version 10.0.0.0我还检查了我的项目文件,以确保没有引用Newtonsoft.Json版本10.0.0.0

What am I missing?我错过了什么?

I think some of your project is referring version 10, while other is referring version 12 of newtonsoft.json.dll.我认为你的一些项目是指版本 10,而其他项目是指 newtonsoft.json.dll 的版本 12。

It seems the v12 DLL is getting copied to output directory.似乎 v12 DLL 正在被复制到输出目录。 The project which refers v10 is throwing this error.引用 v10 的项目抛出此错误。

For all the strong named assemblies, the specific version of DLL is searched in the application directory or GAC.对于所有强命名程序集,在应用程序目录或 GAC 中搜索特定版本的 DLL。 You can overwrite this default behavior by using below configuration file entry - which instructs .net framework to always look for v12 whenever any version from 0 to 12 is called.您可以通过使用以下配置文件条目来覆盖此默认行为 - 它指示 .net 框架在调用 0 到 12 之间的任何版本时始终查找 v12。

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-12.0.1.0" newVersion="12.0.1.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

Hope this helps to resolve your issue.希望这有助于解决您的问题。

I finally got this working.我终于得到了这个工作。 In order to do Assembly Redirect Binding in Azure Functions, it takes a little more effort than a normal .Net application.为了在 Azure Functions 中进行程序集重定向绑定,它比普通的.Net应用程序需要更多的努力。

I followed the following post: https://codopia.wordpress.com/2017/07/21/how-to-fix-the-assembly-binding-redirect-problem-in-azure-functions/我关注了以下帖子: https : //codopia.wordpress.com/2017/07/21/how-to-fix-the-assembly-binding-redirect-problem-in-azure-functions/

Newtonsoft Json while useful is the source of many issues. Newtonsoft Json 虽然很有用,但却是许多问题的根源。

You will need to check all the NuGet packages referenced by every project in your solution to see what version of Newtonsoft Json they require.您需要检查解决方案中每个项目引用的所有 NuGet 包,以了解它们需要什么版本的 Newtonsoft Json。 Some specify a minimal version, in which case you don't have an issue ( like > 10, anything higher than 10 will work ), others require a certain, fixed version ( = 10, must be 10, not less, not higher).有些指定了最小版本,在这种情况下,您没有问题(例如 > 10,任何高于 10 的都可以工作),其他人需要某个固定版本(= 10,必须是 10,而不是更少,而不是更高) . This is up to the author of that particular NuGet package.这取决于该特定 NuGet 包的作者。

When you hit build, the dlls required by the referenced packages will be copied over, then the dlls required by any referenced projects will copied over and it is possible a certain version will be copied over and when you run your application, you get the error you have seen.当您点击构建时,将复制引用包所需的 dll,然后将复制任何引用项目所需的 dll,并且可能会复制某个版本,当您运行应用程序时,您会收到错误消息你见过。

You can check this by looking in the bin folder of your build profile, right click the dll file and inspect its version to see what you have.您可以通过查看构建配置文件的 bin 文件夹来检查这一点,右键单击 dll 文件并检查其版本以查看您拥有的内容。 Assembly redirection should help, when done in the right place, but there is an alternative solution, although it's not always possible.如果在正确的位置完成,程序集重定向应该会有所帮助,但有一个替代解决方案,尽管并非总是可行。

If you can drop the Newtonsoft Json to the minimum version which satisfies all requirements then that is a good way to solve the problem.如果您可以将 Newtonsoft Json 降到满足所有要求的最低版本,那么这是解决问题的好方法。 Chances are you don't need version 12, depending on what you are using from it and depending on what other packages require.您可能不需要版本 12,这取决于您使用它的内容以及其他软件包需要的内容。 You might be able to change all projects to reference the lower version without any side effects.您也许可以将所有项目更改为引用较低版本,而不会产生任何副作用。 If you can, you're golden, just make sure you don't upgrade by mistake.如果可以,你就是金子,只要确保你没有错误升级。 If not, you'll have to do the version redirecting like indicated in other answers.如果没有,您将必须按照其他答案中的指示进行版本重定向。

The basic idea is to make sure, that in your solution, you only use one version of Newtonsoft.基本思想是确保在您的解决方案中,您只使用一个版本的 Newtonsoft。 you can check this package at solution level, there is a consolidate option as well.您可以在解决方案级别检查此包,还有一个合并选项。

So, check all projects in the solution, make sure they all use one version of Newtonsoft and that version is the same across all of them.因此,检查解决方案中的所有项目,确保它们都使用 Newtonsoft 的一个版本,并且该版本在所有项目中都相同。

Once you manage to get everything on one level make sure that:一旦您设法将所有内容都放在一个级别上,请确保:

  1. you delete the Temporary ASp.Net folder contents, in Windows / Microsoft.Net, Framework23 or 64 depends which one you're using.您删除 Temporary ASp.Net 文件夹内容,在 Windows / Microsoft.Net、Framework23 或 64 中取决于您使用的是哪一个。

  2. remove all dlls from all bin folders, a simple Cleanup may not be sufficient.从所有 bin 文件夹中删除所有 dll,简单的清理可能不够。

With all this done, providing you have one version everywhere, you should be able to run you project without issues完成所有这些后,如果您在任何地方都有一个版本,您应该能够毫无问题地运行您的项目

Find out what project trying to load this library.找出尝试加载此库的项目。 Right click on project->Manage Nuget Packages->Find NewtonJson library in installed libs and click Uninstall, maybe this will help.右键单击项目-> 管理 Nuget 包-> 在已安装的库中查找 NewtonJson 库并单击卸载,也许这会有所帮助。 Also like people provided before use could you assembly redirect.也像使用前提供的人一样,您可以进行程序集重定向。

在您使用 Microsoft.IdentityModel.Tokens.Jwt 的地方不要将其添加为页面的使用,而是直接引用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法加载文件或程序集'Newtonsoft.Json,Version = 10.0.0.0' - 但bin文件夹中存在二进制文件 - Could not load file or assembly 'Newtonsoft.Json, Version=10.0.0.0' - However binary exists in bin folder 无法在Azure函数中加载文件或程序集&#39;Newtonsoft.Json,Version = 10.0.0.0,Culture = neutral,PublicKeyToken = 30ad4fe6b2a6aeed&#39; - Could not load file or assembly 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' in Azure Functions Newtonsoft.Json版本8.0.2无法加载文件或程序集错误 - Newtonsoft.Json version 8.0.2 Could not load file or assembly Error 无法加载文件或程序集 &#39;Newtonsoft.Json,版本 = 12.0.0.0? - Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0? 无法加载文件或程序集 &#39;Newtonsoft.Json,版本 = 7.0.0.0 - Could not load file or assembly 'Newtonsoft.Json, Version=7.0.0.0 无法加载文件或程序集Newtonsoft.json版本6.0.0.0 - Could not load file or assembly Newtonsoft.json Version 6.0.0.0 无法加载文件或程序集“Newtonsoft.Json”版本=11.0.0.0 - Could not load file or assembly 'Newtonsoft.Json' Version=11.0.0.0 无法加载文件或程序集'Newtonsoft.Json,版本 = 3.5.0.0 - Could not load file or assembly 'Newtonsoft.Json, Version=3.5.0.0 无法加载文件或程序集“NewtonSoft.Json,版本=12.0.0.0” - Could not load file or assembly 'NewtonSoft.Json, Version=12.0.0.0' 无法加载文件或程序集“ Newtonsoft.Json” - Could not load file or assembly 'Newtonsoft.Json'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM