Error Handling within SignalR Hubs
Firstly, What is SignalR?
ASP.NET SignalR is a .Net library for addding real-time messaging to your applications. It gives you the ability to have your server-side code push content to the connected clients as it happens, in real-time, and back again from clients to a server.
SignalR has its own error handling methods, which can be configured to pass errors to ErrLog.IO.
However, depending on your application scenario, it's possible (and very common) to enable your SignalR functionality across multiple threads, to avoid synchronously locking message processing.
At Kutamo, our SignalR hub has around 6 different threads, to handle different message modalities in parallel. This makes tracking errors a little more tricky.
Native SignalR Hub Error Handling
Aside from wrapping your hub class in a try-catch block, you can create pipeline module to catch incoming errors from SignalR.
public class ErrorHandlingPipelineModule : HubPipelineModule
{
protected override void OnIncomingError(ExceptionContext exceptionContext,
IHubIncomingInvokerContext invokerContext)
{
ErrLog.logger.log(exceptionContext.Error);
if (exceptionContext.Error.InnerException != null)
{
ErrLog.logger.log(exceptionContext.Error.InnerException);
}
base.OnIncomingError(exceptionContext, invokerContext);
}
}
This needs to be instantiated and initialized as part of the MapSignalR() method. This is done by adding a reference to the new pipleline module in your startup.cs file or relevant startup class.
public void Configuration(IAppBuilder app)
{
// Any connection or hub wire up and configuration should go here
GlobalHost.HubPipeline.AddModule(new ErrorHandlingPipelineModule());
app.MapSignalR();
}
Learn more about SignalR
Microsoft and the SignalR maintainers have a great set of documentation and examples over at https://github.com/SignalR/SignalR/
and https://docs.microsoft.com/en-us/aspnet/signalr/overview/