简体   繁体   English

Visual Studio 2012 Web API项目将无法运行 - 找不到Newtonsoft.Json

[英]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. 运行干净的解决方案并重建后,我的MVC 4 Web API项目停止工作。 It's can't find Newtonsoft.Json. 它找不到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. 我知道MS现在使用它作为默认的JSON序列化程序 - 但它不在项目的引用列表中,我不能从NuGet中添加它,因为它已经安装了它。

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. [FileNotFoundException:无法加载文件或程序集'Newtonsoft.Json,Version = 4.5.0.0,Culture = neutral,PublicKeyToken = 30ad4fe6b2a6aeed'或其依赖项之一。 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 d:\\ Data \\ Source Controlled Projects \\ georace \\ georace \\ Server \\ GeoRaceServer \\ MvcWebRole1 \\ Global.asax.cs中的MvcWebRole1.WebApiApplication.Application_Error():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. [HttpException(0x80004005):无法加载文件或程序集“Newtonsoft.Json,Version = 4.5.0.0,Culture = neutral,PublicKeyToken = 30ad4fe6b2a6aeed”或其中一个依赖项。 The system cannot find the file specified.] 该系统找不到指定的文件。]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +12838633 System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context,HttpApplication app)+12838633
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +175 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.HttpApplication.InitSpecial(HttpApplicationState状态,MethodInfo []处理程序,IntPtr appContext,HttpContext上下文)+304
System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +404 System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext,HttpContext context)+404
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +475 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. [HttpException(0x80004005):无法加载文件或程序集“Newtonsoft.Json,Version = 4.5.0.0,Culture = neutral,PublicKeyToken = 30ad4fe6b2a6aeed”或其中一个依赖项。 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 System.Web.HttpRuntime.FirstRequestInit(HttpContext context)+12851296 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context)+159 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr,HttpContext context)+12679949

Ok - found a work around myself, posting in case someone else gets caned by this MS bug. 好的 - 找到一个自己的工作,发布以防其他人被这个MS错误所控制。 The problem is that you can't add a NuGet reference as it's already included in the packages by default. 问题是您无法添加NuGet引用,因为它已默认包含在包中。 So... 所以...

  1. Open up packages.config 打开packages.config
  2. Delete the Newtonsoft.Json entry. 删除Newtonsoft.Json条目。
  3. Save and Build 保存和构建
  4. Re-add Newtonsoft.Json from NuGet. 从NuGet重新添加Newtonsoft.Json。
  5. Build and run 建立并运行

For any package already installed by NuGet that you want to re-install just type the following command in Package Manager Console : 对于NuGet 安装的任何要重新安装的软件包,只需在软件包管理器控制台中键入以下命令:

Update in any project: 在任何项目中更新:

Update-Package Newtonsoft.Json -Reinstall

Update in a specific project 更新特定项目

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

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 对我来说,修复此错误的唯一方法是将缺少的部分添加到我的web.config文件中,我从这里得到了这个答案: 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>

I combined a couple of the options the other SO contributors stated, plus a new one. 我结合了其他SO贡献者所说的几个选项,再加上一个新选项。

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. 首先,事实证明Nuget 2.1有一些问题,这个问题记录在一个错误中,并在2.2.2中修复。 See here: https://nuget.codeplex.com/workitem/3050 请参见: https//nuget.codeplex.com/workitem/3050

So, I did the following steps: 所以,我做了以下步骤:

  1. Updated Nuget version to 2.2.2 as per the link 根据链接将Nuget版本更新为2.2.2
  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) 通过从NUget卸载来删除我项目中对Json的所有引用(右键单击项目并查找包,然后卸载(即使这样做没有)
  3. Physically deleted the Package files from the computer. 从计算机中物理删除了包文件。 I found package files here: //projectdir/packages/Newtonsoft.JSON 我在这里找到了包文件://projectdir/packages/Newtonsoft.JSON
  4. Deleted the packages.config file from the physical directory (//projectname/packages.config) 从物理目录中删除了packages.config文件(//projectname/packages.config)
  5. Once this was all completed, I added Newtonsoft again via Nuget, then did a rebuild, and it worked. 一旦完成这一切,我再次通过Nuget添加了Newtonsoft,然后进行了重建,并且它有效。

I had the same problem (in VS2012). 我遇到了同样的问题(在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" 1)转到工具 - >扩展和更新这里更新(或安装)“NuGet包管理器”接下来转到“在线”并安装“快速添加NuGet”和“NuGet参考”

2) after insttal all in 1) in your project right click to "References" and choose "Manage Nuget Packages" in "Installed packages" find Json.NET. 2)在你的项目中的1)之后,右键单击“References”并选择“Installed packages”中的“Manage Nuget Packages”找到Json.NET。 Click Uninstall - you cannot do this now, but it show you what you must uninstall first (some ASP Web Api client). 单击卸载 - 您现在无法执行此操作,但它会显示您必须首先卸载的内容(某些ASP Web Api客户端)。 So uninstall all of these and try uninstall "Json.NET" again. 所以卸载所有这些并尝试再次卸载“Json.NET”。 Now go to "Online" and install "Json.NET", "ASP.NET WEB API Client Libraries" and "ASP.NET WEB API Client OData". 现在转到“在线”并安装“Json.NET”,“ASP.NET WEB API客户端库”和“ASP.NET WEB API客户端OData”。

3) in Global.asax delete row "WebApiConfig.Register(GlobalConfiguration.Configuration);" 3)在Global.asax中删除行“WebApiConfig.Register(GlobalConfiguration.Configuration);”

build solution and all shoud be ok.. I hope this can help someone :) gl 建立解决方案,一切都好吧..我希望这可以帮助某人:) gl

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. 检查您是否有对JSON.NET的“旧样式”(pre nuget)引用,指向项目中嵌入的过时本地副本。

I had an old Reference\\Lib\\JSON.NET which was in my reference path and getting priority. 我有一个旧的Reference\\Lib\\JSON.NET ,它在我的参考路径中并获得优先权。

Check that the JSON.NET properties page is pointing to the expected Nuget package version. 检查JSON.NET属性页面是否指向预期的Nuget包版本。

I had this problem on a Team Server build which deployed live. 我在实时部署的Team Server构建中遇到此问题。 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. 我不得不修复我的原始解决方案,并进行干净的构建,以确保没有拾取旧的Reference\\Lib\\ DLL。

In my case I checked out a colleague's project which somehow turned my Newtonsoft.Json reference bad - so I had to remove it. 在我的情况下,我检查了一个同事的项目,它以某种方式使我的Newtonsoft.Json参考不好 - 所以我不得不删除它。 Right click on the project -> Manage NuGet Packages. 右键单击项目 - > Manage NuGet Packages。 On the left side click "Installed Packages" then select Json.NET and uninstall it. 在左侧单击“已安装的软件包”,然后选择Json.NET并将其卸载。 In my case I had to uninstall other dependent packages also (Microsoft ASP.Net Web API and others). 在我的情况下,我还必须卸载其他依赖包(Microsoft ASP.Net Web API和其他)。 Choose "Online" on the left side and search for Json.Net, re-install it. 选择左侧的“在线”并搜索Json.Net,重新安装。

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. 我有相同的FileNotFoundException,事实证明我已经在我的项目中引用了Newtonsoft.Json,但“Copy Local”属性未设置为TRUE。 Just set it to TRUE so the dll gets copied to the output directory if this is your case too. 只需将其设置为TRUE即可将dll复制到输出目录中,如果这种情况也是如此。

I tested everything that was said here and nothing worked. 我测试了这里所说的一切,没有任何效果。 In my case I had a path folder like this. 在我的情况下,我有一个像这样的路径文件夹。

  • #Projects/Web/App/... #项目/网络/应用/ ...

The # the cause of my problem. #问题的原因#。 I removed the # and worked fine. 我删除了#并且工作正常。

What caused my problem with Newtonsoft.json was slightly different. 是什么原因导致我的问题与Newtonsoft.json略有不同。 I had developed a Windows app built against .Net 4.5. 我开发了一个针对.Net 4.5构建的Windows应用程序。 I later found out that the customer's environment only supported .Net 4.0. 我后来发现客户的环境只支持.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. 我已将解决方案复制到新区域以保留.Net 4.5工作并将解决方案属性更改为Microsoft.Net 4.0。 I got the error that Newtonsoft.json could not be found and was I missing a reference. 我收到了无法找到Newtonsoft.json的错误,我错过了一个引用。

I followed the procedures stated above for removing the packages.config; 我按照上面描述的过程删除了packages.config; removing Newtonsoft.json; 删除Newtonsoft.json; ensuring that Newtonsoft.json was removed from the \\packages directory; 确保从\\ packages目录中删除了Newtonsoft.json; then reinstalling Newtonsoft.json from NuGet. 然后从NuGet重新安装Newtonsoft.json。 I was able to compile and run the application with .Net 4.0 我能够使用.Net 4.0编译和运行应用程序

Prior to this the packages.config file showed that the version of .Net that Newtonsoft.json targeted was .Net 4.5. 在此之前,packages.config文件显示Newtonsoft.json所针对的.Net版本为.Net 4.5。 Removing and adding the Newtonsoft.json now has the target as .Net 4.0. 删除和添加Newtonsoft.json现在的目标是.Net 4.0。

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. 启动了一个新的VS 2012项目后,我使用NuGet安装了Newtonsoft.JSON,如上所述,已经安装了4.5.6版本,并且看到它已经安装并安装了4.5.10,我印象深刻。

Sweet. 甜。

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. 您可以尝试使用-IgnoreDependencies标志安装SocialAuth.Net软件包,然后手动安装依赖项并确保只安装最新版本的Json.Net库。

Then at runtime the SocialAuth.Net package would respect the assembly redirect and load Json.Net (v4.5) instead of the old version. 然后在运行时,SocialAuth.Net包将尊重程序集重定向并加载Json.Net(v4.5)而不是旧版本。

I had the same problem in VS2012 express, and I managed to install it from 我在VS2012 express中遇到了同样的问题,我设法安装它

TOOLS -> NuGet Package Manager -> Manage NuGet Packages for Solution 工具 - > NuGet包管理器 - >管理解决方案的NuGet包

There I found and installed Newtonsoft Json. 我找到并安装了Newtonsoft Json。 It wasn't installed in none of the projects from solution by then. 到那时,解决方案中没有安装任何项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM