可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
回答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...
- Open up packages.config
- Delete the Newtonsoft.Json entry.
- Save and Build
- Re-add Newtonsoft.Json from NuGet.
- Build and run
回答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
回答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>
回答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:
- Updated Nuget version to 2.2.2 as per the link
- 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)
- Physically deleted the Package files from the computer. I found package files here: //projectdir/packages/Newtonsoft.JSON
- Deleted the packages.config file from the physical directory (//projectname/packages.config)
- Once this was all completed, I added Newtonsoft again via Nuget, then did a rebuild, and it worked.
回答5:
I had the same problem (in VS2012). No answer helped me. Finally I solved it by this:
1)
Go to TOOLS -> EXTENSIONS AND UPDATES
here update (or install) "NuGet Package Manager"
next go to "Online" and install "Quick add NuGet" and "NuGet References"
2) after insttal all in 1)
in your project right click to "References" and choose "Manage Nuget Packages"
in "Installed packages" find Json.NET. Click Uninstall - you cannot do this now, but it show you what you must uninstall first (some ASP Web Api client). So uninstall all of these and try uninstall "Json.NET" again.
Now go to "Online" and install "Json.NET", "ASP.NET WEB API Client Libraries" and "ASP.NET WEB API Client OData".
3) in Global.asax delete row "WebApiConfig.Register(GlobalConfiguration.Configuration);"
build solution and all shoud be ok..
I hope this can help someone :) gl
回答6:
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.
回答7:
In my case I checked out a colleague's project which somehow turned my Newtonsoft.Json reference bad - so I had to remove it. Right click on the project -> Manage NuGet Packages. On the left side click "Installed Packages" then select Json.NET and uninstall it. In my case I had to uninstall other dependent packages also (Microsoft ASP.Net Web API and others). Choose "Online" on the left side and search for Json.Net, re-install it.
回答8:
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.
回答9:
I tested everything that was said here and nothing worked. In my case I had a path folder like this.
The # the cause of my problem. I removed the # and worked fine.
回答10:
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.
回答11:
I did this by mistake, and it sorted it self out fine.
Having started a new VS 2012 project I used NuGet to install Newtonsoft.JSON, as mentioned version 4.5.6 is already installed, and I was impressed to see that this was uninstalled and 4.5.10 installed.
Sweet.
回答12:
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.
回答13:
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.