Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 595
Blaise Avatar asked May 27 '14 15:05

Blaise


1 Answers

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>
like image 135
Gildor Avatar answered Oct 07 '22 03:10

Gildor