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?

Blaise
  • 21,314
  • 28
  • 108
  • 169
  • Are you sure that code is correct and it shouldn't be `WebApiConfig.Register(GlobalConfiguration.Configuration);`? – DavidG May 27 '14 at 15:20
  • That register statement is from http://www.asp.net/web-api/overview/extensibility/configuring-aspnet-web-api#webhost. Is it correct? – Blaise May 27 '14 at 15:21
  • Make that correction as DavidG suggested. Same error. – Blaise May 27 '14 at 15:23

3 Answers3

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>
Gildor
  • 2,484
  • 23
  • 35
  • 1
    Pretty sure that is not the solution. 'cause this statement is there already. But thanks for the input. – Blaise May 27 '14 at 15:35
  • 3
    @Blaise That's wield because I had exactly the same issue (same exception on same line) a few days ago and it was the solution. – Gildor May 27 '14 at 15:44
  • @Blaise Have you tried reinstalling the NuGet package and setting `CopyLocal` to `true`? It's `true` in my project. – Gildor May 28 '14 at 03:11
  • 1
    @Blaise This actually worked for me, but be sure to put in 6.0.0.0 and not the exact version. – MCollard May 30 '14 at 07:57
  • This worked for me except for a minor tweak in that config: – santos Sep 03 '14 at 10:08
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>
Der_Meister
  • 4,771
  • 2
  • 46
  • 53
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.

Blaise
  • 21,314
  • 28
  • 108
  • 169