Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SignalR and 500 Errors

Tags:

signalr

I've been playing around with SignalR very successfully so far, but I've found a situation where I have not really been able to find any documented reference or solution.

I have a server side function with a class parameter that has a Double property

public bool AddThing(Thing thing)
{
    // add and notify client
}

public class Thing {
    public Double Foo { get; set; }
}

The server rightfully returns a 500 error if I send a Thing object with text instead of a number for the property Foo

{"hub":"ThingHub","method":"AddThing","args":[{"Foo":"bar"}],"state":{},"id":1}

Since this happens before the SignalR context kicks in, how can deal with the error at the client side? Does the Hub have any special callbacks or properties to check? Is there anything special I need to do to the Hub on the server side?

Thanks!

like image 824
Jason Avatar asked Jul 22 '12 02:07

Jason


People also ask

How many connections SignalR can handle?

In the default mode, the app server creates five server connections with Azure SignalR Service. The app server uses the Azure SignalR Service SDK by default. In the following performance test results, server connections are increased to 15 (or more for broadcasting and sending a message to a big group).

What is a SignalR error?

This error may be seen if the project contains a folder called SignalR, which will interfere with the automatically-created proxy. To avoid this error, do not use a folder called SignalR in your application, or turn automatic proxy generation off.

How scalable is SignalR?

A SignalR app can scale out based on the number of messages sent, while the Azure SignalR Service scales to handle any number of connections.

Does SignalR need SSL?

If your SignalR application transmits sensitive information between the client and server, use SSL for the transport.


1 Answers

SignalR have some logic on the client side you can use to check if a server side hub method call from the client succeeded or not.

First of all, to handle connection failures you can use the error handler on the hub connection ( https://github.com/SignalR/SignalR/issues/404#issuecomment-6754425 , http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client) like this:

$.connection.hub.error(function() {
    console.log('An error occurred...');
});

So when I recreate your scenario by implementing this on the server side:

public bool AddThing(Thing thing)
{
    return true;
}

public class Thing
{
    public Double Foo { get; set; }
}

.. and then calls this from the client side:

myHub.addThing({"Foo":"Bar"});

The error handling function is invoked, and the text An error occurred... is printed to the console.

Another thing you can do - because invoking a method on the server returns a jQuery deferred object ( https://github.com/SignalR/SignalR/wiki/SignalR-JS-Client-Hubs ) , you can chain a couple of callbacks on the returning object from the invoke. For example the documentation gives this sample:

myHub.someMethod()
     .done(function(result) {
     })
     .fail(function(error) {
     });

It should be noted though that fail is only called when there is an error during hub invocation ( e.g. an exception is thrown inside the server side method ) - https://github.com/SignalR/SignalR/issues/404#issuecomment-6754425.

A final note - I think this is an interesting question because, as I see it, it is the JSON Serializer which throws the exception - e.g., in your case:

Newtonsoft.Json.JsonSerializationException: Error converting value "Bar" to type 'System.Double'. Line 1, position 54. ---> System.FormatException:

... but for sure, it could be interesting if there were any better method than what I described above to handle this scenario - like being able to tell which exact hub invocation caused the 500 Internal Server Error.

like image 131
Lasse Christiansen Avatar answered Sep 19 '22 22:09

Lasse Christiansen