无法加载文件或程序集“Newtonsoft.Json”或其依赖项之一。清单定义与程序集引用不匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22685530/
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-09-03 17:14:23  来源:igfitidea点击:

Could not load file or assembly 'Newtonsoft.Json' or one of its dependencies. Manifest definition does not match the assembly reference

jsonjson.net.net-assembly

提问by noobieDev

Things I've tried after searching:

搜索后我尝试过的事情:

  1. in Web.Config put a binding on the old version:

    <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.1.0" />
    </dependentAssembly>
    
  2. Edit my .csproj file to make sure there is only one Newtonsoft reference

    <Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
       <HintPath>..\packages\Newtonsoft.Json.6.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
       <SpecificVersion>False</SpecificVersion>
       <Private>True</Private>
    </Reference>
    
  3. Search my computer for every Newtonsoft.Json.dll and delete every non 6.0.1 version and delete the contents of my temp folder

  4. Repair/Reinstall the package in nuget manager console

  1. 在 Web.Config 中对旧版本进行绑定:

    <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.1.0" />
    </dependentAssembly>
    
  2. 编辑我的 .csproj 文件以确保只有一个 Newtonsoft 参考

    <Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
       <HintPath>..\packages\Newtonsoft.Json.6.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
       <SpecificVersion>False</SpecificVersion>
       <Private>True</Private>
    </Reference>
    
  3. 在我的计算机上搜索每个 Newtonsoft.Json.dll 并删除每个非 6.0.1 版本并删除我的临时文件夹中的内容

  4. 在 nuget 管理器控制台中修复/重新安装包

It succeeds on building, but gets the error when going to the site.

它构建成功,但在访问站点时出错。

EDIT

编辑

ok, so then I tried to reinstall like every nuget package, and it seems to have added back the 4.5 version of the newtonsoft.json.dll, but I'm getting the same error. My project's Target freamework is .NET 4.5.1 and here is the stack trace I'm getting now:

好的,然后我尝试像每个 nuget 包一样重新安装,它似乎已经添加回了 4.5 版本的 newtonsoft.json.dll,但我遇到了同样的错误。我的项目的目标框架是 .NET 4.5.1,这是我现在得到的堆栈跟踪:

Server Error in '/' Application.

Could not load file or assembly Newtonsoft.Jsonor one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.FileLoadException: Could not load file or assembly Newtonsoft.Jsonor one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Assembly Load Trace: The following information can be helpful to determine why the assembly Newtonsoft.Jsoncould not be loaded.

“/”应用程序中的服务器错误。

无法加载文件或程序集Newtonsoft.Json或其依赖项之一。定位的程序集的清单定义与程序集引用不匹配。(从HRESULT异常:0x80131040

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

异常详细信息::System.IO.FileLoadException无法加载文件或程序集Newtonsoft.Json或其依赖项之一。定位的程序集的清单定义与程序集引用不匹配。(从HRESULT异常:0x80131040

源错误:

执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常来源和位置的信息。

程序集加载跟踪:以下信息有助于确定Newtonsoft.Json无法加载程序集的原因。

=== Pre-bind state information ===
LOG: DisplayName = Newtonsoft.Json
(Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: Newtonsoft.Json | Domain ID: 2
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///c:/users/user/documents/visual studio 2013/Projects/foo/bar/
LOG: Initial PrivatePath = c:\users\user\documents\visual studio 2013\Projects\foo\bar\bin
Calling assembly : (Unknown).
 ===
LOG: This bind starts in default load context.
LOG: Using application configuration file: c:\users\user\documents\visual studio 2013\Projects\foo\bar\web.config
LOG: Using host configuration file: C:\Users\user\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Users/user/AppData/Local/Temp/1/Temporary ASP.NET Files/root/48686d37/9d7a6572/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Users/user/AppData/Local/Temp/1/Temporary ASP.NET Files/root/48686d37/9d7a6572/Newtonsoft.Json/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///c:/users/user/documents/visual studio 2013/Projects/foo/bar/bin/Newtonsoft.Json.DLL.
LOG: Using application configuration file: c:\users\user\documents\visual studio 2013\Projects\foo\bar\web.config
LOG: Using host configuration file: C:\Users\user\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Redirect found in application configuration file: 4.5.0.0 redirected to 6.0.1.0.
LOG: Post-policy reference: Newtonsoft.Json, Version=6.0.1.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
LOG: Attempting download of new URL file:///C:/Users/user/AppData/Local/Temp/1/Temporary ASP.NET Files/root/48686d37/9d7a6572/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///C:/Users/user/AppData/Local/Temp/1/Temporary ASP.NET Files/root/48686d37/9d7a6572/Newtonsoft.Json/Newtonsoft.Json.DLL.
LOG: Attempting download of new URL file:///c:/users/user/documents/visual studio 2013/Projects/foo/bar/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.

Stack Trace:

堆栈跟踪:

[FileLoadException: 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)]

[FileLoadException: Could not load file or assembly 'Newtonsoft.Json, Version=6.0.1.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)]
       System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +0
       System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +34
       System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +152
       System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection) +77
       System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +16
       System.Reflection.Assembly.Load(String assemblyString) +28
       System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +38

[ConfigurationErrorsException: Could not load file or assembly 'Newtonsoft.Json, Version=6.0.1.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)]
       System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +752
       System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +218
       System.Web.Configuration.CompilationSection.LoadAssembly(AssemblyInfo ai) +130
        System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +170
       System.Web.Compilation.BuildManager.GetPreStartInitMethodsFromReferencedAssemblies() +91
       System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +285
       System.Web.Compilation.BuildManager.ExecutePreAppStart() +153
       System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +516

[HttpException (0x80004005): Could not load file or assembly 'Newtonsoft.Json, Version=6.0.1.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)]
       System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9913572
       System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
       System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18408

采纳答案by noobieDev

Ok, I think I got it to work now. I deleted every Newtonsoft.Json.dll on my machine that wasn't the latest version that I could find, made sure I had the latest version in NuGet, and build it and made sure that was the latest one in the bin folder, and I left the changes in the web.config and the .csproj. Now I'm on to another error, so it must be working..

好的,我想我现在可以开始工作了。我删除了我机器上所有不是我能找到的最新版本的 Newtonsoft.Json.dll,确保我在 NuGet 中有最新版本,并构建它并确保它是 bin 文件夹中的最新版本,并且我保留了 web.config 和 .csproj 中的更改。现在我遇到了另一个错误,所以它必须工作..

回答by user1477388

To solve this, I ensured all my projects used the same version by running the following command and checking the results:

为了解决这个问题,我通过运行以下命令并检查结果来确保我的所有项目都使用相同的版本:

update-package Newtonsoft.Json -reinstall

And, lastly I removed the following from my web.config:

而且,最后我从我的 web.config 中删除了以下内容:

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

If you want to ensure all your Newtonsoft.Json packages are the sameversion, you can specify the version like so:

如果你想确保你所有的 Newtonsoft.Json 包都是相同的版本,你可以像这样指定版本:

update-package Newtonsoft.Json -version 6.0.0 -reinstall

回答by CraigV

I am using Newtonsoft.Json v6.0.3, but this is what I had to do in my Web.config file:

我使用的是 Newtonsoft.Json v6.0.3,但这是我必须在我的 Web.config 文件中执行的操作:

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

Note that even though I am using 6.0.3, I had to put in newVersion="6.0.0.0"

请注意,即使我使用的是 6.0.3,我也必须输入 newVersion="6.0.0.0"

In my packages.config file I have:

在我的 packages.config 文件中,我有:

<package id="Newtonsoft.Json" version="6.0.3" targetFramework="net45" />

回答by Samuel

I tried the following after having already ensured that my computer had the same version in all locations and that my projects were all pointing to the same reference path. I had also made sure that the binding of the old version was their and bound to the current version of dll that I had.

在确保我的计算机在所有位置具有相同的版本并且我的项目都指向相同的参考路径之后,我尝试了以下操作。我还确保旧版本的绑定是他们的并且绑定到我拥有的当前版本的 dll。

I work in an environment with a strict framework and the framework team often upset the versioning with the different dll's.

我在一个具有严格框架的环境中工作,框架团队经常用不同的 dll 破坏版本控制。

How I fixed this issue was to run the package manager console within visual studio (2013). From there I ran the following command:

我解决这个问题的方法是在 Visual Studio (2013) 中运行包管理器控制台。从那里我运行了以下命令:

update-package Newtonsoft.Json -reinstall

followed by

其次是

update-package Newtonsoft.Json

This went through and updated all of my config files and relevant project files. Forcing them all to the same version of the dll. Which was initially version 4.5 before updating again to get the latest.

这通过并更新了我的所有配置文件和相关项目文件。强制它们都使用相同版本的 dll。最初是 4.5 版,然后再次更新以获取最新版本。

回答by S__

I had the same error message and, like you mentioned, it was due to different versions of the Newtonsoft.Json.dll being referenced.

我有同样的错误消息,就像你提到的,这是由于引用了不同版本的 Newtonsoft.Json.dll。

Some projects in my MVC solution used the NuGet package for version 4 of that dll.

我的 MVC 解决方案中的一些项目使用了该 dll 版本 4 的 NuGet 包。

I then added a NuGet package (for Salesforce in my case) that brought Newtonsoft.Json version 6 with it as a dependency to one of the projects. That was what triggered the problem for me.

然后我添加了一个 NuGet 包(在我的例子中用于 Salesforce),它将 Newtonsoft.Json 版本 6 作为一个项目的依赖项。这就是引发我问题的原因。

To clean things up, I used the Updates section in the NuGet Package Manager for the solution (off Tools menu or solution right-click) to update the Json.Net package throughout the solution so it was the same version for all projects.

为了清理,我使用 NuGet 包管理器中的更新部分作为解决方案(关闭工具菜单或解决方案右键单击)来更新整个解决方案中的 Json.Net 包,因此它对于所有项目都是相同的版本。

After that I just checked the App Config files to ensure any binding redirect lines were going to my chosen version as below.

之后,我只检查了应用程序配置文件,以确保任何绑定重定向行都转到我选择的版本,如下所示。

<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />

回答by Willy David Jr

By commenting it out this part on my web.config solved my problem:

通过在我的 web.config 上注释掉这部分解决了我的问题:

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

But of course you need to make sure you have updated or you have the right version by doing this in your package manager console:

但是当然,您需要通过在包管理器控制台中执行此操作来确保已更新或拥有正确的版本:

update-package Newtonsoft.Json -reinstall

回答by Jineesh Uvantavida

You can solve the issue by adding below lines in web.config file.

您可以通过在 web.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-8.0.0.0" newVersion="8.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

回答by Matthew Cavallo

I have tried the steps by Oleg, and they worked for my same situation.

我已经尝试过 Oleg 的步骤,它们适用于我的相同情况。

Steps:

脚步:

  1. Run update-package Newtonsoft.Json -reinstallin Package Manager.

  2. Delete your binby enabling viewing the hidden files and deleting the binfolder.

  3. Close your Visual Studio and re-open it.

  4. Now run your project again. I believe it should be ok!

  1. update-package Newtonsoft.Json -reinstall在包管理器中运行。

  2. bin通过启用查看隐藏文件和删除bin文件夹来删除您的。

  3. 关闭 Visual Studio 并重新打开它。

  4. 现在再次运行您的项目。我相信应该没问题!

回答by Goda Kotb

from Tools>>NuGet Package Manager>>Manage Package for solution update Newtonsoft.Json of all solutions to latest Version

从工具>>NuGet 包管理器>>管理解决方案包将所有解决方案的 Newtonsoft.Json 更新到最新版本

回答by Shaulian

What actually helped me was to turn off the Resharper buildand to use the VisualStudio Re-Build option on my project.

实际上帮助我的是关闭Resharper 构建并在我的项目中使用 VisualStudio Re-Build 选项。