我在 StackOverflow 上搜索了许多与 SignalR 连接相关的其他问题,但似乎都不适用于我的具体情况。
我有一个使用 SignalR 集线器的应用程序。客户端可以使用两种方法连接到集线器:
- 通过使用底层客户端连接到集线器的.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);
...
}
SignalR 客户端
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 周后,连接失败并且doesn't恢复后,我可以在日志中看到错误:
Error when sending transaction to SignalRClient from /api/heartbeat: The 'InvokeCoreAsync' method cannot be called if the connection is not active
我不明白这是怎么发生的,因为我正在使用_hubConnection.Closed
方法中的SignalRClient
处理连接关闭时的情况,然后执行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.
错误?如果是这样的话,可能是什么原因造成的?
非常感谢任何为我指明正确方向的建议。