Visual Studio 2012 Web API project won't run -

2019-01-10 08:11发布

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

13条回答
手持菜刀,她持情操
2楼-- · 2019-01-10 08:25

What caused my problem with Newtonsoft.json was slightly different. I had developed a Windows app built against .Net 4.5. I later found out that the customer's environment only supported .Net 4.0. I had copied the solution to a new area to preserve the .Net 4.5 work and changed the Solution Properties to Microsoft.Net 4.0. I got the error that Newtonsoft.json could not be found and was I missing a reference.

I followed the procedures stated above for removing the packages.config; removing Newtonsoft.json; ensuring that Newtonsoft.json was removed from the \packages directory; then reinstalling Newtonsoft.json from NuGet. I was able to compile and run the application with .Net 4.0

Prior to this the packages.config file showed that the version of .Net that Newtonsoft.json targeted was .Net 4.5. Removing and adding the Newtonsoft.json now has the target as .Net 4.0.

查看更多
混吃等死
3楼-- · 2019-01-10 08:27

Check to see if you had an 'old style' (pre nuget) reference to JSON.NET pointing to an out of date local copy embedded in your project.

I had an old Reference\Lib\JSON.NET which was in my reference path and getting priority.

Check that the JSON.NET properties page is pointing to the expected Nuget package version.

I had this problem on a Team Server build which deployed live. I had to fix my original solution and do a clean build to make sure the old Reference\Lib\ DLL wasn't being picked up.

查看更多
疯言疯语
4楼-- · 2019-01-10 08:29

I was having the same FileNotFoundException and it turns out that I already have the reference to Newtonsoft.Json on my project, BUT the "Copy Local" property wasn't set to TRUE. Just set it to TRUE so the dll gets copied to the output directory if this is your case too.

查看更多
贪生不怕死
5楼-- · 2019-01-10 08:32

You could try to install the SocialAuth.Net package with the ‑IgnoreDependencies flag, then installing the dependencies manually and making sure that you only install the latest version of Json.Net library.

Then at runtime the SocialAuth.Net package would respect the assembly redirect and load Json.Net (v4.5) instead of the old version.

查看更多
Summer. ? 凉城
6楼-- · 2019-01-10 08:35

I had the same problem in VS2012 express, and I managed to install it from

TOOLS -> NuGet Package Manager -> Manage NuGet Packages for Solution

There I found and installed Newtonsoft Json. It wasn't installed in none of the projects from solution by then.

查看更多
Explosion°爆炸
7楼-- · 2019-01-10 08:39

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.
查看更多
登录 后发表回答