Could not load file or assembly ‘Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed’ or one of its dependencies.

I wanted to parse some JSON via Json.Decode (System.Web.Helpers.dll) in my app and I was also using HttpContentExtensions.ReadAsAsync method, but I was getting the following error:

An exception of type ‘System.IO.FileNotFoundException’ occurred in mscorlib.dll but was not handled in user code. 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 system cannot find the file specified.

Hmm, that’s not very helpful. After little googling and finding that no solution provided works, I started digging.

I had a reference to System.Net.Http.Formatting assembly in my project, because HttpContentExtensions.ReadAsAsync is in that assembly:

references

Fuslogvw showed following during app runtime:

LOG: DisplayName = Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
 (Fully-specified)
LOG: Appbase = ...
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = ...
Calling assembly : System.Net.Http.Formatting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: ...
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL .....
LOG: All probing URLs attempted and failed.

See that row with “Calling assembly”? Is that the culprit? Indeed it is:

httpformatting

This kind of dependency is horrible. Current version of https://www.nuget.org/packages/newtonsoft.json/ is 7.0.1!

Assembly redirect to the rescue!

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