【发布时间】:2020-09-23 00:15:53
【问题描述】:
我在 StackOverflow 上搜索了许多与 SignalR 连接相关的其他问题,但似乎没有一个适用于我的具体案例。
我有一个使用 SignalR 集线器的应用程序。客户端可以使用 2 种方法连接到集线器:
- 通过使用底层客户端连接到集线器的 .NET Core API
- 直接连接到集线器的 URL
我遇到的问题是使用 .NET Core API 进行连接(方法 1)。当服务器端应用程序运行很长时间(可能是 2 周)时,API 使用的 SignalR 连接失败。与 SignalR 集线器的直接连接(方法 2)继续有效。
以下是通过 API 进行连接的方式:
.NET Core Web API
[Route("~/api/heartbeat")]
[HttpPost]
public async Task SendHeartbeat(nodeId) {
await SignalRClient.SendHeartbeat(nodeId);
...
}
SignalRClient
public static class SignalRClient
{
private static HubConnection _hubConnection;
/// <summary>
/// Static SignalRHub client - to ensure that a single connection to the SignalRHub is re-used,
/// and to prevent excessive connections that cause SignalR to fail
/// </summary>
static SignalRClient()
{
string signalRHubUrl = "...someUrl";
_hubConnection = new HubConnectionBuilder()
.WithUrl(signalRHubUrl)
.Build();
_hubConnection.Closed += async (error) =>
{
Log.Error("SignalR hub connection was closed - reconnecting. Error message - " + error.Message);
await Task.Delay(new Random().Next(0, 5) * 1000);
try
{
Log.Error("About to reconnect");
await _hubConnection.StartAsync();
Log.Error("Reconnect now requested");
}
catch (Exception ex)
{
Log.Error("Failed to restart connection to SignalR hub, following a disconnection: " + ex.Message);
}
};
InitializeConnection();
}
private static async void InitializeConnection()
{
try
{
Log.Information("Checking hub connection status");
if (_hubConnection.State == HubConnectionState.Disconnected)
{
Log.Information($"Starting SignalRClient using signalRHubUrl");
await _hubConnection.StartAsync();
Log.Information("SignalRClient started successfully");
}
}
catch (Exception ex)
{
Log.Error("Failed to start connection to SignalRClient : " + ex.Message + ", " + ex.InnerException.Message);
}
}
public static async Task SendHeartbeat(string nodeId)
{
try
{
Log.Information("Attempting to send heartbeat to SignalRHub");
await _hubConnection.InvokeAsync("SendNodeHeartbeatToMonitors", nodeId);
}
catch (Exception ex)
{
Log.Error($"Error when sending heartbeat to SignalRClient for NodeId: {nodeId}. Error: {ex.Message}");
}
}
在正常运行大约 2 周后,连接失败并且没有恢复,我可以在日志中看到一个错误:
Error when sending transaction to SignalRClient from /api/heartbeat: The 'InvokeCoreAsync' method cannot be called if the connection is not active
我不明白这是怎么回事,因为我使用SignalRClient 中的_hubConnection.Closed 方法来处理连接关闭时的情况,然后执行await _hubConnection.StartAsync(); 以重新启动连接,如如上面的代码所示。
连接定期由于某种原因被关闭(每 30 分钟一次),但它通常会恢复连接,并且我在日志中看到以下错误:
SignalR hub connection was closed - reconnecting. Error message - The remote party closed the WebSocket connection without completing the close handshake.
这表明代码已成功进入_hubConnection.Closed 方法(因为这是我记录该消息的地方),因此看起来连接通常已成功重新启动。
那么,为什么有时连接完全失败,但又无法重新启动?我想知道我是否以合理的方式连接到 SignalR 集线器(特别是,我想知道为 SignalRClient 使用静态类是否是一个很好的模式)。我想知道我的实际问题是否是所有这些The remote party closed the WebSocket connection without completing the close handshake. 错误?如果是这种情况,可能是什么原因造成的?
非常感谢任何为我指明正确方向的建议。
【问题讨论】:
标签: c# asp.net asp.net-core asp.net-core-webapi signalr-hub