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/



Recent Posts

Jan, 2022
Dec, 2021
Nov, 2021
Oct, 2021
Sep, 2021
Aug, 2021
Jun, 2021
Jan, 2021
Mar, 2020
Feb, 2020
Jun, 2018
May, 2018
Feb, 2018
Jan, 2018
Dec, 2017
Nov, 2017
Oct, 2017
Sep, 2017

 

Discuss this article