How to force WebAPI to use JSON.net 6.0.3 instead of 4.5?
Asked Answered
B

3

15

After adding WebAPI and register it in Global.asax.

We find our web app breaks at this line:

Line 17:             GlobalConfiguration.Configure(WebApiConfig.Register);

Error message:

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.

After some checkup, I find we are using Json.net 6 in this MVC 5.1 application. Does it mean we have to downgrade to Json.net 4.5 for WebAPI to work?


In my .csproj file, there is only one entry:

<Reference Include="Newtonsoft.Json, Version=6.0.3.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\packages\Newtonsoft.Json.6.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
  <Private>False</Private>
</Reference>

When I look into my Json.NET in Manage NuGet Packages, it also says my Json.NET is version 6.0.3.

In addition, there is already the bindingRedirect statement in my web.config.

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

But, if I look into the references of the web project inside visual studio. The path of Newtonsoft.Json points to C:\Program Files (x86)\Microsoft Visual Studio 12.0\Blend\Newtonsoft.Json.dll but Copy Local is false.

How can that be? How can we handle this conflict?

Baptist answered 27/5, 2014 at 15:16 Comment(3)
Are you sure that code is correct and it shouldn't be WebApiConfig.Register(GlobalConfiguration.Configuration);?Adrell
That register statement is from asp.net/web-api/overview/extensibility/…. Is it correct?Baptist
Make that correction as DavidG suggested. Same error.Baptist
P
11

You need to add a binding redirection in your web.config (possibly merge with your existing binding redirections):

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <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>
  </assemblyBinding>
</runtime>
Pb answered 27/5, 2014 at 15:31 Comment(5)
Pretty sure that is not the solution. 'cause this statement is there already. But thanks for the input.Baptist
@Baptist That's wield because I had exactly the same issue (same exception on same line) a few days ago and it was the solution.Pb
@Baptist Have you tried reinstalling the NuGet package and setting CopyLocal to true? It's true in my project.Pb
@Baptist This actually worked for me, but be sure to put in 6.0.0.0 and not the exact version.Remonstrate
This worked for me except for a minor tweak in that config: <bindingRedirect oldVersion="4.5.0.0" newVersion="6.0.0.0" />Leucas
F
5

The redirect did not work for me until I update the Web Api to a recent version:

PM> update-package Microsoft.AspNet.WebApi.Client -Version 4.0.30506
Updating 'Microsoft.AspNet.WebApi.Client' from version '4.0.20710.0' to '4.0.30506.0' in project 'TestProject.Api'.

  <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>
Fargone answered 18/8, 2014 at 9:48 Comment(0)
B
2

Okay, here is my fix.

Well one thing I don't know is how the Json.net reference points to the dll in the Blend folder in the first place.

I tried to re-NuGet but found it rather inconvenient because WebApi and WebGrease are all dependent on it.

So I just went ahead and deleted that reference. That of course breaks everything related.

When adding the reference back, I simply Add Reference by browsing to the dll under the /.package folder inside this project.

It works!

Pretty brutal? Just make sure we checked

  • .csproj
  • Web.Config
  • the property in the Reference entry in VS

Dare to try after all bases are covered.

Baptist answered 28/5, 2014 at 18:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.