Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SIgnalR: How to send exceptions to client on connect?

In the hub i want to authenticate the clients based on Connection Header.
If the client is not allowed to connect, i want to throw an Exception with the detailed error.
But, if i throw an exception on the hub, the client only receives "(500) Internal Server Error"

public override Task OnConnected()
{
    string clientKey = Context.Headers["clientKey"];
    string version = Context.Headers["version"];
    if (!this.isValid(clientKey, version))
        throw new InvalidOperationException("SIGNALR: Invalid client");

    return base.OnConnected();
}

What i have to do to send the exception properly?

Thanks!

like image 805
nuno.filipesf Avatar asked Oct 15 '25 14:10

nuno.filipesf


2 Answers

When error is going to happen you can return that error or return your custom message to the client and based of that message you can redirect the client to Error page or display dialog that indicates that Error has happened.

Don't throw the error on the server side. Return the error on the client side and notify the user

like image 147
Martin Kostovski Avatar answered Oct 18 '25 05:10

Martin Kostovski


To enable sending error details to clients you should enable this in your Hub configuration. add this line of code in your startup class.

var hubConfiguration = new HubConfiguration {EnableDetailedErrors = true};
app.MapSignalR(hubConfiguration);
like image 30
Shahin Mohammadi Avatar answered Oct 18 '25 04:10

Shahin Mohammadi