In Web API all the exceptions can be cought by Middleware. In Asp.Net Core 5.0 Hub Filters will do that job.
But how to handle exceptions in Asp.Net Core 3.1 in SignalR hubs? Is there a sole way to write try/catch in every methods like below?
[Authorize]
public class OrdersHub : BaseHub
{
public async Task GetOrder(Guid requestId, int orderId)
{
try
{
var data = await ordersService.GetOrderAsync(orderId);
await Clients.Caller.SendAsync("GetOrderResult", requestId, result);
}
catch (Exception ex)
{
await Clients.Caller.Reject(requestId, ex);
}
}
}
You may know that ASP.NET SignalR has support for HubPipeline modules that provide a way to handle hub exceptions globally.
But from this doc we can find HubPipeline modules is no longer supported in ASP.NET Core SignalR, and currently it seems not provide alternative approach to handle incomming errors globally.
As you mentioned, we can wrap code in try-catch blocks and log the exception object or manually send it to caller.
Please note that exceptions often contain sensitive information, for security reasons sending detailed information to clients is not recommended in production.
Besides, ASP.NET Core SignalR provides built-in diagnostics logging feature that could help capture and log useful transports and Hub related information, which could help troubleshoot the issue.
Note: you can check this github issue that discuss same requirement about "Signalr .net core central exception handling".