Home > Back-end >  How to attach error in invocation completion with SignalR?
How to attach error in invocation completion with SignalR?

Time:03-28

I have a SignalR implementation in .NET with a custom IHubProtocol that simply utilizes JsonHubProtocol.

I noticed that my SignalR server sends a message of type CompletionMessage along with the invocation ID, as well as other useful properties such as Error to indicate if there was an error, and also HasResult.

How can I "throw" an error when the invocation happens, so that the Error property will contain this error, so the client can handle it properly?

CodePudding user response:

After a lot of trial an error, I found out that my LogFilter which logs all my invocations was also catching all exceptions and logging them as errors, which prevented the SignalR error propagating mechanism from working properly. To fix this, I re-threw the exception after catching and logging.

That finally gave me client errors. However, I still didn't know what errors they were. To solve this, you have to enable an option to provide detailed errors when adding SignalR to your application services like so:

services.AddSignalR(o =>
            {
                // Provide detailed error messages only for non-production environments.
                o.EnableDetailedErrors = !HostEnvironment.IsProduction();
            });

The difference between detailed errors and non-detailed errors -- and this part is not properly documented anywhere -- is that a detailed error will give you the exception type and message.

  • Related