我在一个目标为.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.
}