17

My Windows service is in the same solution as a MVC project.

The MVC project uses a reference to SignalR Client which requires Newtonsoft.Json v6 +

the Windows service uses System.Net.Http.Formatting, which requires Newtonsoft.Json version 4.5.0.0.

I assumed this would not be a problem, as I could just use a binding redirect in my App.Config, however I get an error of

An unhandled exception of type 'System.IO.FileLoadException' occurred in System.Net.Http.Formatting.dll

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

my app.config has the following:

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

I added that myself, and it does not work, I have also tried uninstalling and re-installing Json.Net with the nuget package manager, to no avail

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
LiamHT
  • 1,312
  • 3
  • 12
  • 28

3 Answers3

6

We faced same error and struggled to fix for few days. We finally we found this post on stack overflow Assembly reference cannot be resolved - dependentAssembly issue?

This made us realize to look into the version of System.Net.Http.Formatting being used and we found that our solution had been using multiple version of System.Net.Http.Formatting.dll and those each of them were referencing different version of Newtonsoft.Json.dll.

Removing references of older version of System.Net.Http.Formatting and adding references back, fixed the problem.

Hope that helps.

SharpC
  • 6,974
  • 4
  • 45
  • 40
chintan123
  • 348
  • 3
  • 10
  • I had to remove the NuGet package, along with a package that it depended upon, delete the Newtonsoft DLL from my web site's Bin directory, and remove the assembly info from the bottom of web.config. Then I installed the _depending_ NuGet package, which pulled in its favorite version of Newtonsoft. Then everything worked. – crenshaw-dev May 09 '18 at 17:49
1

Does the assemblyBinding tag have proper xmlns schema? Check if the issue you are encountering is same as Assembly binding redirect does not work

Community
  • 1
  • 1
Mothupally
  • 750
  • 2
  • 8
  • 16
  • i have no xmlns written on the assembly binding as it should work without it nevermind, that worked. You saved me hours, thanks :) – LiamHT Jun 22 '15 at 10:01
0

@chintan123 actually pointed my in the right direction, turns out that despite the class library I was creating had a reference to both System.Net.Http.Formatting and also Newtonsoft.Json, only the former was being copied to the bin directory of the calling project that needed it.

Adding a reference to Newtonsoft.Json to the main calling project fixed the issue.

SharpC
  • 6,974
  • 4
  • 45
  • 40