Visual Studio 2012 Web API project won't run - can't find Newtonsoft.Json

After running a clean solution and rebuild, my MVC 4 Web API project stops working. It's can't find Newtonsoft.Json.

I know that MS is using this as the default JSON serializer now - but its not in the references list for the project, and I can't add it from NuGet as it says it's already installed.

Does anyone have any idea here as to what could be going wrong?

Full stack trace:

[FileNotFoundException: 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.]
MvcWebRole1.WebApiApplication.Application_Error() in d:\Data\Source Controlled Projects\georace\georace\Server\GeoRaceServer\MvcWebRole1\Global.asax.cs:70

[HttpException (0x80004005): 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.]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +12838633
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +175
System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +304
System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +404
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +475

[HttpException (0x80004005): 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.]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +12851296 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +159 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +12679949


Solution 1:

Ok - found a work around myself, posting in case someone else gets caned by this MS bug. The problem is that you can't add a NuGet reference as it's already included in the packages by default. So...

  1. Open up packages.config
  2. Delete the Newtonsoft.Json entry.
  3. Save and Build
  4. Re-add Newtonsoft.Json from NuGet.
  5. Build and run

Solution 2:

For any package already installed by NuGet that you want to re-install just type the following command in Package Manager Console:

Update in any project:

Update-Package Newtonsoft.Json -Reinstall

Update in a specific project

Update-Package Newtonsoft.Json -Reinstall -Project My.App

Solution 3:

For me the only thing that fixed this error was to add the missing section to my web.config file, which I got from this answer here: https://stackoverflow.com/a/20414714/940783

Here is what I needed to add:

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

Solution 4:

I combined a couple of the options the other SO contributors stated, plus a new one.

First, It turns out that Nuget 2.1 had some issues, and this issue was logged in a bug, and was fixed in 2.2.2. See here: https://nuget.codeplex.com/workitem/3050

So, I did the following steps:

  1. Updated Nuget version to 2.2.2 as per the link
  2. Removed all references to Json in my project by uninstalling from NUget (right click project and find package, and uninstall (Even this didn't do it)
  3. Physically deleted the Package files from the computer. I found package files here: //projectdir/packages/Newtonsoft.JSON
  4. Deleted the packages.config file from the physical directory (//projectname/packages.config)
  5. Once this was all completed, I added Newtonsoft again via Nuget, then did a rebuild, and it worked.