SignalR:无法使用任何可用的传输连接到服务器

4

我在一个目标为.Net 4.6.2的WPF应用程序中使用Microsoft.AspNetCore.SignalR.Client(1.1.0版本)。我正在进行客户端设置,如下所示:

public class SignalRClient
{
   private readonly HubConnection hubConnection;

   public SignalRClient()
   {
      this.hubConnection = new HubConnectionBuilder()
                .WithUrl("http://localhost:8089/MyDataHub")
                .Build();
   }

   // I'm calling the following method through a callback function in Autofac container after the DI container is built (but I tried doing this directly in the ctor and I got the same issue).

   public void Initialize()
   {
      Task.Factory.StartNew(
           () => 
           {
               // This is where I get the error.
               this.hubConnection.StartAsync().Wait();
           })
   }
}

服务器端日志显示:

16:56:45.380  Info | Request starting HTTP/1.1 GET http://localhost:8089/MyDataHub?id=7qLNfG-iV1Xr3oN_r8U_nA
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 POST http://localhost:8089/MyDataHub/negotiate  0
16:56:45.540  Info | Request starting HTTP/1.1 POST http://localhost:8089/MyDataHub/negotiate  0
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 11.2669ms 200 application/json
16:56:45.552  Info | Request finished in 11.2669ms 200 application/json
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 POST http://localhost:8089/MyDataHub/negotiate  0
16:56:45.703  Info | Request starting HTTP/1.1 POST http://localhost:8089/MyDataHub/negotiate  0
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 7.7109ms 200 application/json
16:56:45.703  Info | Request finished in 7.7109ms 200 application/json
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:8089/MyDataHub?id=4TN4k9wZWXsZ5iPyGNKSOA
16:56:45.719  Info | Request starting HTTP/1.1 GET http://localhost:8089/MyDataHub?id=4TN4k9wZWXsZ5iPyGNKSOA
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 6.7847ms 200 application/octet-stream
16:56:45.719  Info | Request finished in 6.7847ms 200 application/octet-stream
fail: Microsoft.AspNetCore.SignalR.HubConnectionContext[5]
      Failed connection handshake.
16:56:51.013 Error | Failed connection handshake.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 20012.9772ms 101
16:57:05.393  Info | Request finished in 20012.9772ms 101

客户端错误信息:
Unable to connect to the server with any of the available transports

我不太理解这个错误的含义以及如何修复它。看起来服务器和客户端无法就传输协议达成一致?
请注意,服务器正在使用Microsoft.AspNet.SignalR(2.4.0v)。服务器项目是一个Asp.Net Core 2.1应用程序(我无法在此更改任何内容)。我正在尝试使用AspNet Core版本的客户端(因为当我测试一些其他针对完全相同框架版本的.Net Framework WPF和控制台项目时,我没有收到此错误)。
可能导致此特定错误的原因是什么?
可能的解决方法:
编辑:
服务器中的路由映射:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
   app.UseSignalR(routes => { routes.MapHub<MyDataHub>("/MyDataHub"); });
}

编辑 2:

MyDataHub.cs 类:

public class MyDataHub : Hub<IMyDataHub>
{
   public async Task SendMessage(MyData data)
   {
      await this.Clients.All.SendMessage(data);
   }

   // Some other business methods here defined in `IMyDataHub` interface that are meant to be invoked by clients.
}

你在Startup类中映射了你的Hub路由吗? - MindSwipe
@MindSwipe 我也这么认为。我已经将它作为问题的编辑添加了进去。 - kovac
@PabloRecalde 你指的是哪里? - kovac
@PabloRecalde 看起来这没关系。我刚试了一下我的应用,使用 http 而不是 https,HubConnectionBuilder 足够智能,自动为我进行了规范化处理。 - MindSwipe
我明白了,抱歉打扰了。 - Pablo Recalde
显示剩余3条评论
1个回答

0
问题实际上与ODE本身没有太大关系。事实证明,解决方案中的不同项目(有很多!)使用了不同版本的System.Threading.Tasks.Extensions包(有些使用4.3.0,而另一些使用4.5.1)。并且在app.config中存在一个绑定重定向。
<dependentAssembly>
    <assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="sdhke3jdj" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" />
</dependentAssembly>

这句话并不是很清晰,因为我在任何地方都没有 4.1.0。所以,我首先通过升级到 4.5.1,将整个解决方案的 System.Threading.Tasks.Extensions 统一起来。然后从 app.config 文件中删除了上述绑定重定向,然后它开始工作了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接