简体   繁体   中英

Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0 issue using Google.Apis.*

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

Doing my head in trying to find this reference. I am running a .NET Framework v4.5 project, and then importing the DLL into an aspx page.

The DLL contains a class used to connect to the Google APIs - specifically for the Google Calendar.

I've tried removing all the NuGet references and re-installing. I've ensured they are all updated to the latest stable version. The Newton.JSON library is specifically set to v13.0.1.

My packages.config file is as follows:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Google.Apis" version="1.51.0" targetFramework="net45" />
  <package id="Google.Apis.Auth" version="1.51.0" targetFramework="net45" />
  <package id="Google.Apis.Calendar.v3" version="1.51.0.2312" targetFramework="net45" />
  <package id="Google.Apis.Core" version="1.51.0" targetFramework="net45" />
  <package id="Newtonsoft.Json" version="13.0.1" targetFramework="net45" />
  <package id="System.Net.Http" version="4.0.0" targetFramework="net45" />
</packages>

I've seen others suggesting updating the web.config file in the following folder: C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config I have no idea why though. There are no references to the Newton.JSON library in there.

Where to next?

EDIT: I have resolved this as recommended below by editing the web.config of the website to point old references to the new one, however I now receive the following error:

Could not load type 'System.Reflection.IntrospectionExtensions' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

This is usually bequase one of the other packages references Newton.JSON version 12.0.0.0 directly.(In this case probably the google apis packages)
As you add 13.0.1 to you project the other packages cant find the reference.
You can add the following bit to the config:

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

What this does is, when you code or one of the packages references a version of Newtonsoft.Json between version 0 and 13 it will now reference version 13

Look into the.csproj file it should also been updated v13

sample

 <ItemGroup>
    <Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>..\..\..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
    </Reference>
    <Reference Include="System.Net.Http" />
  </ItemGroup>

and make sure the dependent Assembly in web config also has been updated

Sample:

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

I had this same problem and found it important to note something else. Connor's answer was correct but my users still had this issue. My app is a WPF application whereas even though I had this change in my app.config they did not have it in theirs. It took a couple hours to track down while it was failing for them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM