7

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

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
stev-e
  • 436
  • 6
  • 15
  • Does the PublicKeyToken for Newtonsoft.Json in the error message match what's in your app.config? – Gabriel Luci Feb 23 '16 at 14:09
  • Hi, thanks for the advice but yes, the publicKeyToken in the error message is the same one as in the assemblyIdentity Tag. – stev-e Feb 23 '16 at 15:52
  • Did you try all the suggestions at https://stackoverflow.com/questions/3490327/assembly-binding-redirect-does-not-work ? – danio Jun 30 '17 at 14:27

3 Answers3

1

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.

  • Hi, as mentioned in my original post, the assembly binding section looks exactly like your improved one. And the reference of Newtonsoft.Json points to \packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll with local copy set to true. I wonder why the test routing works very well and the productive application fails (same machine and I copied the libraries from build folder, where no failure occurs during the tests, to the productive folder, where the error occurs... ). – stev-e Feb 23 '16 at 19:42
  • @stev-e where you able to solve it? if so, how? thank you. – Marco Alves Mar 20 '17 at 20:33
  • (Five years later...) Yup, me too. This issue is alive and kicking, even with bindingRedirect in place.... – Mike Gledhill Aug 22 '22 at 13:23
0

Delete the old ref and get new dll by nuget,I think this will resolve most scene.

Tom
  • 11
  • 2
-1

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)

O. Jones
  • 103,626
  • 17
  • 118
  • 172
Dan011093
  • 1
  • 1
  • Hi, the reference is working, no yellow icon there. Version 6 is not possible, it needs to be >= 8 – stev-e Feb 23 '16 at 20:21