c# - Newtonsoft.Json DLL 版本与 signalR + WEBAPI 冲突

标签 c# asp.net-web-api json.net signalr

我收到错误:

Could not load file or assembly 'Newtonsoft.Json' or one of its dependencies. Manifest definition does not match the assembly reference.

将 WebApi Controller 类添加到我的 ASP.NET MVC 项目后。我的项目中也有 SignalR。两者都使用 Newtonsoft.Json,但似乎它们没有引用相同的版本。

SignalR 使用并与 6.0.8 版本配合使用,而错误告诉我 MapHttpRoute :

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        RouteTable.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}");
    }

尝试加载4.5版本。

我读了这篇文章SignalR & WebApi - colliding Newtonsoft.Json references

但我不知道如何告诉 webapi 指向 6.0.8 版本。

最佳答案

您可以使用 AssemblyBinding 重定向到已安装的 Newtonsoft.Json 程序集。只需添加到您的 Web.config

</configuration>
  <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.8" newVersion="6.0.8" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

这会将所有错误版本的调用重定向到您安装的版本 6.0.8

关于c# - Newtonsoft.Json DLL 版本与 signalR + WEBAPI 冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39794088/

相关文章:

c# - 使用 System.Net.HttpClient 上传时如何跟踪进度?

wcf - Json.net 无法反序列化复杂类型列表

json - 使用 Newtonsoft 反序列化 JSON 属性(当它可以是单个字符串或 JARRAY 时)

c# - 从不同的 IEnumerable 映射 KeyValuePair

c# - 如何将 Lambda 表达式输出转换为 List<T>

c# - Visual Studio Publish, “PublishTelemetry” 任务无法加载

C# web-api post 使用两个参数运行

security - 使用 JWT 进行应用身份验证和授权

c# - 如何使用 Web api 和 Angular 从字节下载文件

c# - 如何解析会导致非法 C# 标识符的 JSON 字符串?