SignalR和WebApi - 碰撞Newtonsoft.Json引用

Mat*_*asK 12 asp.net-web-api signalr.client

我有一个连接到SignalR 2.0后端的Windows客户端,现在我也试图让它连接到ASP.NET Web API 2.1后端.

我用来与web api通信的库依赖于Newtonsoft.Json 6.0.0.0,但Microsoft.AspNet.SignalR.Client 2.0.0.0似乎依赖于Newtonsoft.Json 4.5.0.0.

由于这是一个Windows客户端,因此dll文件最终位于同一目录中,并且只有两个所需版本中的一个将最终在磁盘上,使得signalr-client或HttpClient无法加载其版本的Newtonsoft.Json dll文件.

如果6.*版本在磁盘上,则SignalR错误:

  Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0,
  Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its 
  dependencies. The located assembly's manifest definition does not match the 
  assembly reference.(Exception from HRESULT: 0x80131040)
Run Code Online (Sandbox Code Playgroud)

是否有可能使SignalR使用较新版本的Newtonsoft或能够在同一解决方案中使用这两个版本?

请记住,我对Nuget包装和SignalR似乎使用的隐式引用非常新...

解决方案: 我用版本6.0替换了signalR客户端库中的Newtonsoft引用,并将下面由Kiran Challa编写的程序集重定向添加到库的App.config以及可执行项目的app.config,现在这两个库寻找并加载相同的组件.

Kir*_*lla 19

Web API和SignalR是参考Json.net的4.5汇编版本构建的.如果您想使用6.0版本的Json.net,则需要在配置文件中添加程序集绑定重定向.

<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.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
Run Code Online (Sandbox Code Playgroud)