首页 > 解决方案 > SignalR .NET 客户端无法完成握手

问题描述

我正在使用 AspNetCore.SignalR 1.1.0 和 AspNetCore.SignalR.Client 1.1.0。我有一个 .NET 客户端(WPF 应用程序)在连接到在 localhost 上运行的 Web 应用程序时可以正常工作,但是当我尝试连接到在暂存环境(Azure 上的 IIS)中运行的同一个 Web 应用程序时,它无法完成握手。我是 SignalR 的新手,无法确定失败的原因。

这是 .NET 客户端中的连接:

//Create the connection
connection = new HubConnectionBuilder()
    .WithUrl(await ClientDataStore.ParseRouteAsync(Routes.POSFullSync), options =>
    {
        options.AccessTokenProvider = async () => await Task.FromResult(await ClientDataStore.GetAccessTokenAsync());
        options.Headers.Add("X-Vending-Outlet-Id", KeyApplicationVariables.VOInstanceId.ToString());
    })
    .ConfigureLogging(logging => {
        logging.SetMinimumLevel(LogLevel.Trace);
        logging.AddConsole();
    })
    .Build();

await connection.StartAsync();
//Fails here with Operation was cancelled

这是Web应用程序上的配置

//Add signalR
services.AddSignalR(hubOptions =>
{
    hubOptions.EnableDetailedErrors = true;
    hubOptions.KeepAliveInterval = TimeSpan.FromMinutes(1);
});

//Configure signalr
app.UseSignalR(routes =>
{
    routes.MapHub<POSFullSyncHub>(Routes.POSFullSync);
    routes.MapHub<POSPartialSyncHub>(Routes.POSPartialSync);
});

我启用了 SignalR 日志记录并使用 Fiddler 进行了查看,但似乎客户端只是在握手完成之前停止。

这是客户端的一些日志记录

dbug: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[18]
      Transport 'WebSockets' started.
info: Microsoft.AspNetCore.Http.Connections.Client.HttpConnection[3]
      HttpConnection Started.
info: Microsoft.AspNetCore.SignalR.Client.HubConnection[24]
      Using HubProtocol 'json v1'.
dbug: Microsoft.AspNetCore.SignalR.Client.HubConnection[28]
      Sending Hub Handshake.
dbug: Microsoft.AspNetCore.Http.Connections.Client.Internal.WebSocketsTransport[13]
      Received message from application. Payload size: 32.
The thread 0x3ca4 has exited with code 0 (0x0).
fail: Microsoft.AspNetCore.SignalR.Client.HubConnection[35]
      Error processing the handshake response.
System.OperationCanceledException: The operation was canceled.
   at System.Threading.CancellationToken.ThrowOperationCanceledException()
   at System.IO.Pipelines.Pipe.GetReadResult(ReadResult& result)
   at System.IO.Pipelines.Pipe.GetReadAsyncResult()
   at System.IO.Pipelines.Pipe.DefaultPipeReader.GetResult(Int16 token)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at Microsoft.AspNetCore.SignalR.Client.HubConnection.<HandshakeAsync>d__61.MoveNext()
fail: Microsoft.AspNetCore.SignalR.Client.HubConnection[43]
      Error starting connection.
System.OperationCanceledException: The operation was canceled.
   at System.Threading.CancellationToken.ThrowOperationCanceledException()
   at System.IO.Pipelines.Pipe.GetReadResult(ReadResult& result)
   at System.IO.Pipelines.Pipe.GetReadAsyncResult()
   at System.IO.Pipelines.Pipe.DefaultPipeReader.GetResult(Int16 token)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at Microsoft.AspNetCore.SignalR.Client.HubConnection.<HandshakeAsync>d__61.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.SignalR.Client.HubConnection.<StartAsyncCore>d__47.MoveNext()

它适用于 VS 中的 localhost IIS Express,但看起来故障出在客户端。当我在 VS 调试模式下运行相同的 WPF 应用程序但连接到 Azure 上 IIS 10 上的相同应用程序版本时,会发生故障。我错过了什么?

如何确定无法完成握手的原因?

更新:

我已经设法确定有一些与网络相关的可能导致问题的东西,即它在我的家庭网络上工作,但在现场使用网络时不起作用。由于我不拥有此应用程序失败的网络,因此我可能需要向管理员指定要求。SignalR 的网络要求是什么?我怎么能证实这个理论?

标签: c#signalrsignalr-client

解决方案


推荐阅读