Darryl Wagoner WA1GON
Darryl Wagoner WA1GON

Reputation: 1029

Handling logic Error hub on SignalR request best practice

I love SignalR and currently doing a few apps in it. Since SignalR is async if a logic error occurs on the service what is the best practice to handle that error?

I have been doing invoke to the client with an error object and adding it to a Reactive Extension observable, but that seems a little awkward.

The client is .Net Core and Blazor.

Suggestions?

Upvotes: 0

Views: 577

Answers (1)

Fei Han
Fei Han

Reputation: 27803

SignalR is async if a logic error occurs on the service what is the best practice to handle that error?

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.

Besides, from this doc, we can find:

Exceptions often contain sensitive information, such as connection information. Because of this, SignalR does not expose the details of exceptions that occur on the server to the client. However, instances of HubExceptionare sent to the client.

If you'd like to handle logic error occurs in Hub method, you can try to wrap the code logic in a try-catch block, then manually write exception in log.

Or you can create a new HubException instance with specified error message, which would be detected by client invoking this hub method.

try
{
    //code logic here
    //...

    //...
    //Convert.ToDateTime("hello");

    await Clients.All.SendAsync("ReceiveMessage", user, message);
}
catch (Exception ex)
{

    throw new HubException(ex.Message);
} 

Upvotes: 1

Related Questions