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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12027543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 20:07:25  来源:igfitidea点击:

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

c#visual-studio-2012asp.net-web-api

提问by BlackSpy

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

运行干净的解决方案并重建后,我的 MVC 4 Web API 项目停止工作。找不到 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. 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

[FileNotFoundException:无法加载文件或程序集“Newtonsoft.Json,版本=4.5.0.0,Culture=neutral,PublicKeyToken=30ad4fe6b2a6aeed”或其依赖项之一。系统找不到指定的文件。]
MvcWebRole1.WebApiApplication.Application_Error() in d:\Data\Source Controlled Projects\georace\georace\Server\GeoRaceServer\MvcWebRole1\Global.asax.cs:70

[HttpException (0x80004005): 无法加载文件或程序集“Newtonsoft.Json,版本=4.5.0.0,Culture=neutral,PublicKeyToken=30ad4fe6b2a6aeed”或其依赖项之一。系统找不到指定的文件。]
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): 无法加载文件或程序集“Newtonsoft.Json,版本=4.5.0.0,Culture=neutral,PublicKeyToken=30ad4fe6b2a6aeed”或其依赖项之一。系统找不到指定的文件。]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +12851296 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +159 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) + 12679949

采纳答案by BlackSpy

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

好的 - 在我自己周围找到了一个工作,发布以防其他人被这个 MS 错误折磨。问题是您无法添加 NuGet 引用,因为默认情况下它已包含在包中。所以...

  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
  1. 打开packages.config
  2. 删除 Newtonsoft.Json 条目。
  3. 保存和构建
  4. 从 NuGet 重新添加 Newtonsoft.Json。
  5. 构建并运行

回答by zeristor

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.

甜的。

回答by Simon_Weaver

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

我在实时部署的 Team Server 版本上遇到了这个问题。我必须修复我的原始解决方案并进行干净的构建以确保旧的Reference\Lib\DLL 没有被拾取。

回答by Tony D

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.

就我而言,我检查了一个同事的项目,该项目以某种方式使我的 Newtonsoft.Json 引用变坏了 - 所以我不得不将其删除。右键单击项目 -> 管理 NuGet 包。在左侧单击“Installed Packages”,然后选择 Json.NET 并卸载它。在我的情况下,我还必须卸载其他依赖包(Microsoft ASP.Net Web API 和其他)。在左侧选择“在线”,搜索Json.Net,重新安装。

回答by Rubens Mariuzzo

For any package alreadyinstalled 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

回答by MunsterMan

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. See here: https://nuget.codeplex.com/workitem/3050

首先,事实证明 Nuget 2.1 有一些问题,这个问题被记录在一个错误中,并在 2.2.2 中修复。请参阅此处: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.
  1. 根据链接将 Nuget 版本更新为 2.2.2
  2. 通过从 NUget 卸载(右键单击项目并找到包,然后卸载(即使没有这样做),删除了我项目中对 Json 的所有引用
  3. 从计算机中物理删除了包文件。我在这里找到了包文件://projectdir/packages/Newtonsoft.JSON
  4. 从物理目录(//projectname/packages.config)中删除了packages.config文件
  5. 全部完成后,我通过 Nuget 再次添加了 Newtonsoft,然后进行了重建,并且成功了。

回答by jfrowies

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.

我遇到了相同的 FileNotFoundException ,结果我已经在我的项目中引用了 Newtonsoft.Json,但是“Copy Local”属性没有设置为 TRUE。如果您的情况也是如此,只需将其设置为 TRUE 即可将 dll 复制到输出目录。

回答by ClearCloud8

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>

回答by Nikunj B. Balar

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) 而不是旧版本。

回答by mvurban

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.

#我的问题的原因。我删除了 # 并且工作正常。