Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0 in combination with Microsoft.AspNet.WebApi.Client

5

I’m coding a class library (called mylibrary.dll) that itself references some more libraries -> uses the following DLLs (taken from package.config for version overview):

<package id="EntityFramework" version="6.0.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
  <package id="Newtonsoft.Json" version="8.0.2" targetFramework="net45" />
  <package id="System.Data.SQLite" version="1.0.99.0" targetFramework="net45" />
  <package id="System.Data.SQLite.Core" version="1.0.99.0" targetFramework="net45" />
  <package id="System.Data.SQLite.EF6" version="1.0.99.0" targetFramework="net45" />
  <package id="System.Data.SQLite.Linq" version="1.0.99.0" targetFramework="net45" />
  <package id="UnmanagedExports" version="1.2.7" targetFramework="net45" />`

mylibrary.dll is a wrapper that exposes some managed code to a caller that expects unmanaged code (in other words where native DLL entries are expected).

If I test the public interface of mylibrary.dll via NUnit test methods there is no error at all. But if I call same methods via the same interface from the targetapplication.exe I recognize the following situations:

  • Test method A: Calls a simple JSON to string operation (makes use of Newtonsoft.JSON library) and runs just fine.
  • Test method B: Calls a method that does a PostAsync and furthermore

var vResponseObject = await vResponse.Content.ReadAsAsync<ApiResponse<T>>().ConfigureAwait(false);

Behind the scenes the ReadAsAsync call uses Newtonsoft.JSON to deserialize the object of type <T>. It seems that this function is the one that generates the error:

Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=...........' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

HTTPContent.ReadAsAsync is provided by Microsoft.AspNet.WebApi.Client (extends System.Net.Http) that itself depends on Newtonsoft.JSON Version 6.0.x (see NuGet dependency for this package). Version 6.0.x is not installed, instead version 8.0.x. So there is a need for assebly binding redirection that is managed in app.config:

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

Now I don’t know how to solve this. In my project Microsoft.AspNet.WebApi.Client is the only one of the other libraries that references Newtonsoft.JSON (version 6.0.x, the same version the error tells me). It seems that the binding redirect is simply ignored. Because it is no “File not found exception” I think it is able to locate version 8 of the dll but expected 6, right? All the DLLs are in the same directory as the targetapplication.exe

Update with a partial solution: As a workaround I was able to avoid the external call of Newtonsoft.Json through System.Net.Http.Formatting.dll

//vResponseObject = await vResponse.Content.ReadAsAsync<ApiResponse<T>>().ConfigureAwait(false);
string vStr = await vResponse.Content.ReadAsStringAsync();
vResponseObject = JsonConvert.DeserializeObject<ApiResponse<T>>(vStr);

But this is really no valid solution for further developement if I have to code around a calls like mydll -> thirdparty.dll -> anotherthirdparty.dll

c#
dll
reference
json.net
.net-assembly
asked on Stack Overflow Feb 23, 2016 by stev-e • edited Feb 28, 2016 by Mr Lister

2 Answers

0

Change

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

for

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

And check in your dependencies Newtonsoft.Json. if it has the yellow icon, delete it and add it manually. (Make sure to add 6.0 version). (You should have the dll in your project folder, or some other project). If that doesnt work add the old version through NuGet (I preffer adding them manually myself).

The location of the dll is usually: C:\Users\Username\Documents\Visual Studio 2013\Projects\ProjectFolder\packages\Newtonsoft.Json.6.0.8\lib\net45 (or whatever version of .net)

answered on Stack Overflow Feb 23, 2016 by Dan011093 • edited Jan 25, 2018 by O. Jones
0

I just solved this problem with Newtonsoft using version 7.0.1. I replaced the old binding in my web.config file, which was oddly:

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

To:

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

Finally, change the Newtonsoft reference from whatever it is pointing to, to the version in your NuGet Package, v8. You are most likely pointing to one of many Newton.Json DLLs, i.e. C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Stack 5\Packages\Newtonsoft.Json.6.0.3\lib\net40. I have no idea why it does this but I have only had issues with this library.

answered on Stack Overflow Feb 23, 2016 by John VandenBrook

User contributions licensed under CC BY-SA 3.0