我在面向.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和Console项目进行测试时,我没有收到此错误)。
编辑:
服务器中的路由映射:
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.
}
答案 0 :(得分: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文件中删除上述绑定重定向,它开始工作。