Failed to load file or assembly "Newtonsoft.Json, Version = 6.0.0.0" in conjunction with Microsoft.AspNet.WebApi.Client

Im encoding a class library (called mylibrary.dll), which itself references some other libraries β†’ uses the following DLLs (taken from package.config to review the version):

<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 shell that provides some managed code to a caller who expects unmanaged code (in other words, where entries from the embedded DLL are expected).

If I test the open interface mylibrary.dll using the NUnit testing methods, there is no error at all. But if I call the same methods through the same interface from the targetapplication.exe file, I recognize the following situations:

  • Test Method A: Calling a simple JSON operation for a string (used by Newtonsoft.JSON) and works fine.
  • Test Method B: Calls the method that PostAsync does and, in addition,

var vResponseObject = wait vResponse.Content.ReadAsAsync <ApiResponse <T β†’ () ConfigureAwait (false) ;.

Behind the scenes, the ReadAsAsync call uses Newtonsoft.JSON to deserialize an object of type <T> . This function seems to be generating an error:

Failed to load file or assembly "Newtonsoft.Json, Version = 6.0.0.0, Culture = neutral, PublicKeyToken = ..........." or one of its dependencies. The located assembly 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), which itself depends on Newtonsoft.JSON version 6.0.x (see the NuGet dependency for this package). Version 6.0.x is not installed, instead of version 8.0.x. Thus, there is a need for assebly binding redirection, which is controlled 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 do not know how to solve this. In my project, Microsoft.AspNet.WebApi.Client is the only other library that references Newtonsoft.JSON (version 6.0.x, the same version that the error tells me about). It seems that binding redirection is simply ignored. Because it is not a β€œFile Exception Not Found”, I think it can find version 8 of the dll, but 6 is expected, right? All DLLs are in the same directory as targetapplication.exe

Update with a partial solution: As a workaround, I was able to avoid an external call to Newtonsoft.Json via 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 not a valid solution for further development, if I need to code calls like mydll β†’ thirdparty.dll β†’ anotherthirdparty.dll

+5
source share
2 answers

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 your dependencies with Newtonsoft.Json. if it has a yellow icon, delete it and add it manually. (Be sure to add version 6.0). (You should have a dll in the project folder or in some other project). If this does not work, add the old version via NuGet (I prefer to add them manually myself).

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

0
source

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

 <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 link to everything it refers to, the version in your NuGet package, v8. Most likely, you pointed to one of the many Newton.Json DLL files, 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 this is happening, but I only had problems with this library.

0
source

Source: https://habr.com/ru/post/1243669/


All Articles