Blaise
Blaise

Reputation: 22232

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

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?

Upvotes: 15

Views: 10591

Answers (3)

Der_Meister
Der_Meister

Reputation: 5027

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>

Upvotes: 5

Blaise
Blaise

Reputation: 22232

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.

Upvotes: 2

Gildor
Gildor

Reputation: 2574

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>

Upvotes: 11

Related Questions