【发布时间】: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 和控制台项目进行测试时,我没有收到此错误)。
- 此特定错误的可能原因是什么?
- 可能的修复。
编辑:
服务器中的路由映射:
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 按照您的要求更新了问题。