【问题标题】:How to force WebAPI to use JSON.net 6.0.3 instead of 4.5?如何强制 WebAPI 使用 JSON.net 6.0.3 而不是 4.5?
【发布时间】:2014-07-16 14:01:45
【问题描述】:

添加WebAPI并在Global.asax注册后。

我们在这一行发现我们的网络应用中断:

Line 17:             GlobalConfiguration.Configure(WebApiConfig.Register);

错误信息:

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.

经过一番检查,我发现我们在这个MVC 5.1 应用程序中使用了Json.net 6。这是否意味着我们必须降级到Json.net 4.5 才能使WebAPI 工作?


在我的.csproj 文件中,只有一个条目:

<Reference Include="Newtonsoft.Json, Version=6.0.3.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
  <SpecificVersion>False</SpecificVersion>
  <HintPath>..\packages\Newtonsoft.Json.6.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
  <Private>False</Private>
</Reference>

当我在Manage NuGet Packages 中查看我的Json.NET 时,它还说我的Json.NET 是6.0.3 版。

另外,我的web.config中已经有bindingRedirect语句了。

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

但是,如果我查看 Visual Studio 中 Web 项目的 referencesNewtonsoft.Json 的路径指向C:\Program Files (x86)\Microsoft Visual Studio 12.0\Blend\Newtonsoft.Json.dllCopy Local 为假。

怎么可能?我们如何处理这种冲突?

【问题讨论】:

  • 您确定代码正确且不应该是WebApiConfig.Register(GlobalConfiguration.Configuration);
  • 该注册语句来自asp.net/web-api/overview/extensibility/…。对吗?
  • 按照 DavidG 的建议进行更正。同样的错误。

标签: c# json.net asp.net-web-api2 asp.net-mvc-5.1


【解决方案1】:

您需要在 web.config 中添加绑定重定向(可能与现有的绑定重定向合并):

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <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>
  </assemblyBinding>
</runtime>

【讨论】:

  • 很确定这不是解决方案。因为这个声明已经存在了。但感谢您的意见。
  • @Blaise 这很有效,因为几天前我遇到了完全相同的问题(同一行上的相同异常),这就是解决方案。
  • @Blaise 您是否尝试过重新安装 NuGet 包并将CopyLocal 设置为true?在我的项目中是true
  • @Blaise 这实际上对我有用,但一定要输入 6.0.0.0 而不是确切的版本。
  • 这对我有用,只是在该配置中做了一个小调整:
【解决方案2】:

在我将 Web Api 更新到最新版本之前,重定向对我不起作用:

PM> update-package Microsoft.AspNet.WebApi.Client -Version 4.0.30506
Updating 'Microsoft.AspNet.WebApi.Client' from version '4.0.20710.0' to '4.0.30506.0' in project 'TestProject.Api'.

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

【讨论】:

    【解决方案3】:

    好的,这是我的解决方法。

    我不知道的一件事是,Json.net 引用首先是如何指向 Blend 文件夹中的 dll。

    我尝试重新NuGet,但发现它很不方便,因为WebApi和WebGrease都依赖它。

    所以我继续删除了该引用。这当然会破坏所有相关的内容。

    在添加引用时,我只是通过浏览到此项目内/.package 文件夹下的 dll 来添加引用。

    有效!

    相当残忍?只要确保我们检查过

    • .csproj
    • Web.Config
    • VS中Reference条目中的属性

    在所有基地都覆盖后敢于尝试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-09
      • 2014-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-13
      • 2023-02-19
      相关资源
      最近更新 更多