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

264

我尝试过的事情:

  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引用

  3. <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>
    
  4. 搜索我的计算机中的每个 Newtonsoft.Json.dll 文件,删除所有非 6.0.1 版本并删除临时文件夹中的内容

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

  6. 构建成功,但在访问网站时出现错误。

    编辑

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

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

    无法加载文件或程序集 '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.
    

    堆栈跟踪:

    [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
    

2
好的,我想现在它可以工作了。我删除了机器上除了最新版本之外的所有Newtonsoft.Json.dll,确保我在NuGet中有最新版本,并构建它并确保它是bin文件夹中的最新版本,然后我留下了web.config和.csproj中的更改。现在我遇到了另一个错误,所以它一定正在工作... - noobieDev
1
将答案添加到您的问题中,以便人们可以看到已解决,并将其标记为解决方案。 - Per Hornshøj-Schierbeck
2
我今天(2016年3月1日)使用8.0.2版本仍然遇到了这个问题的某个版本。它试图加载6.0.0版本。我的解决方案中没有文件引用6.0.0。没有任何版本为6.0.0的.dll文件在我的解决方案区域中。我在计算机上有许多newtonsoft.json.dll的副本,它们是由安装的第三方软件使用的,我不想搞乱它们。除了删除所有这些副本之外,我已经尝试了整个线程中提到的每个解决方案,但没有任何作用。 - Shavais
1
我也遇到了newtonsoft-json的这个错误信息,当我将一个项目的引用添加到另一个项目时,但是项目中的.Net Framework版本不同,将两个项目的.Net Framework版本设置为相同后问题得到解决。但我无法理解为什么会显示newtonsoft-json相关的错误信息而不是Framework错误信息。 - Sameer
1
在所有项目中安装相同版本的Newtonsoft.Json,即使它们没有使用它。 - rubStackOverflow
显示剩余5条评论
36个回答

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

为确保我的所有项目使用相同的版本,我运行了以下命令并检查了结果:

update-package Newtonsoft.Json -reinstall

最后,我从我的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>

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

如下:

update-package Newtonsoft.Json -version 6.0.0 -reinstall

注意:要在Visual Studio中打开PMC,请点击“工具”->“NuGet程序包管理器”->“程序包管理器控制台”。感谢@Rose!


22
重新运行安装命令对我来说起作用了,我不需要修改web.config文件。 - Keith
5
我只需要添加<dependentAssembly>标签就可以消除这个错误。 - Nicklas Møller Jepsen
4
为了让它在我的情况下正常工作,我必须提供-version,但省略-reinstall标志。 - anre
3
我正在使用最近的版本9.0.1,但它仍然显示相同的错误。我随后检查了csproj文件中的版本号,发现是9.0.0.0,将其添加到我的web.config中,问题得以解决。
  1. 确保csproj文件中的版本与web.config文件中的版本相同。
  2. 注意版本号,它可能会显示x.0.1,实际版本是x.0.0.0,其中x可以是6或9,或者是其他任何数字。
- Eugene
10
请注意,在VS2017中运行该命令,您可以进入“工具”->“Nuget包管理器”->“程序包管理器控制台”。 - Rose
显示剩余9条评论

93
我正在使用Newtonsoft.Json v6.0.3,但我需要在我的Web.config文件中进行以下配置:

我正在使用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>
请确认即使我正在使用6.0.3版本,我仍需要添加newVersion="6.0.0.0"。 在我的packages.config文件中,我有:
<package id="Newtonsoft.Json" version="6.0.3" targetFramework="net45" />

1
只是一条注释,我尝试卸载/删除/清除所有与Newtonsoft.Json有关的内容,然后使用Nuget重新安装它,但仍然收到上面提到的错误。直到我进行了我提到的更改后,才成功地使其正常工作。 - CraigV
我曾经遇到过这个问题,并使用程序集绑定日志查看器来帮助诊断。程序集绑定器报告v6.0.3 Newtonsoft.Json.dll为v6.0.0.0,因此当我将其配置为6.0.3时,重定向会失败。 - Jason Slocomb
"Note"这一行帮了我很大的忙。最初在bindingRedirect中,新版本newVersion是9.0.1.19813,因为DLL文件上是这样写的。现在我的代码如下: False - Mark Seefeldt
三年后,我有了同样的经历。将新版本从“12.0.1.0”更改为“12.0.0.0”解决了问题。 - Andrew Shepherd

70

在确认我的电脑各个位置都安装了相同版本并且项目都指向相同的参考路径后,我尝试了以下解决方法。我也确保旧版本的绑定存在,并绑定到了我当前拥有的dll的版本。

我在一个严格框架的环境中工作,框架团队经常会使用不同的dll来扰乱版本控制。

我是通过在Visual Studio(2013)中运行包管理器控制台来解决这个问题的。然后我运行了以下命令:

update-package Newtonsoft.Json -reinstall

跟随着

update-package Newtonsoft.Json

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


这个更新解决了我的问题。我的项目已经升级到Newtonsoft 7.0版本。 - id.ot
对我有用。在这样做之后,重要的是要确保您的 package.config 和 web.config 文件具有匹配的版本。 - TheValyreanGroup
从NuGet包管理器控制台完成后,对我很有效! - Kbdavis07

25

我遇到了相同的错误信息,就像你提到的一样,这是由于有不同版本的Newtonsoft.Json.dll被引用导致的。

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

然后,我向其中一个项目添加了一个NuGet软件包(在我的情况下是Salesforce),它作为一个依赖项带来了Newtonsoft.Json 6版本。 这就是导致我的问题的原因。

为了清理事物,我使用NuGet软件包管理器中的更新部分(在工具菜单或解决方案右键单击之外)来更新整个解决方案的Json.Net软件包,以使所有项目使用相同的版本。

之后,我只需检查App Config文件以确保任何绑定重定向行将转到我的选择版本,如下所示。

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

如果同一MVC应用程序的DLL具有不同版本的Newtonsoft.Json依赖项,该怎么办?这就是我卡住的地方。 - Jeremy Ray Brown
使用NuGet包管理器中的Consolidate选项,确保版本不会分离。 - niico

19

好的,我想我现在已经让它工作了。我删除了我电脑上不是最新版本的Newtonsoft.Json.dll,确保我在NuGet中有最新版本,并构建它并确保它是bin文件夹中的最新版本,我还保留了web.config和.csproj文件中的更改。现在我遇到了另一个错误,所以它必须正在工作。


2
你是如何找到计算机上所有位置的? - obaylis
当我从团队基础服务器获取项目到我的电脑时,我也遇到了错误。我在解决方案中添加了Newtonsoft的引用,但它没有起作用。然后我通过NuGet包管理器安装了Json.net(Newtonsoft.json)。现在它可以正常工作了。 - Mohammad Sadiqur Rahman
它在bin文件夹里面。 文件名——Newtonsoft.Json.dll 删除此文件并重新安装包,或者只安装较低版本并使用NuGet-Solution重新安装正确版本。 - cain

8

在经过数小时的苦战后,我采取了以下措施:

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-12.0.0.0" newVersion="12.0.0.0" />
    </dependentAssembly>

    <!-- other assemblies... -->

  </assemblyBinding>
</runtime>

正确输入新版本和旧版本号的关键是打开项目的 References,找到包并进入其属性或点击 alt + enter

您会发现一个名为Version的区域,在我的情况下是12.0.0.0,而实际上在使用 Manage NuGet 进行探索时应该是12.0.3

因此,您需要从引用属性中选择包的版本(在我的情况下为12.0.0.0

最后,cleanrebuild 项目(您可能需要先删除 binobj 文件夹)。

您可能会遇到其他软件包依赖问题,对于我所遇到的所有问题都执行了相同的操作,并得到了解决。


删除 bin 和 obj 文件夹对我起了作用。 - IAmCoder

7

通过注释掉我的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>

当然,您需要通过在软件包管理器控制台中执行以下操作来确保已更新或具有正确的版本:
update-package Newtonsoft.Json -reinstall

6
您可以通过在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>

5

在我的情况下,这只是一个问题:

Tools -> NuGet Package Manager -> Package Manager Settings -> Clear Cache

问题是由于我重新映射了TFS文件夹而引起的。

4

我已经尝试了Oleg提供的步骤,他们适用于我相同的情况。

步骤:

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

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

  3. 关闭Visual Studio,然后重新打开它。

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


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