【问题标题】:SignalR: Unable to connect to the server with any of the available transportsSignalR:无法使用任何可用的传输连接到服务器
【发布时间】:2019-07-16 09:04:30
【问题描述】:

我在面向 .Net 4.6.2 的 WPF 应用程序中使用 Microsoft.AspNetCore.SignalR.Client (1.1.0v)。我正在设置客户端:

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 和控制台项目进行测试时,我没有收到此错误)。

  1. 此特定错误的可能原因是什么?
  2. 可能的修复。

编辑:

服务器中的路由映射:

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 我想是的。我将其添加为对问题的编辑。
  • 您能否也添加您的MyDataHub 课程中的代码?
  • 我认为您正在混合使用 http 和 https?
  • @MindSwipe 按照您的要求更新了问题。

标签: c# .net wpf signalr


【解决方案1】:

事实证明,这个问题与颂歌本身没有太大关系。事实证明,解决方案中的不同项目(而且有很多!)正在使用不同版本的 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。因此,我首先通过将 System.Threading.Tasks.Extensions 升级到 4.5.1 来整合整个解决方案。然后从 app.config 文件中删除上述绑定重定向,它就开始工作了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    • 2017-12-29
    相关资源
    最近更新 更多