Andrew Stephens
Andrew Stephens

Reputation: 10203

Handling exceptions in SignalR .Net client

I have the following client-side method (.Net client):

public void CallServer()
{
    try
    {
        _hubProxy.Invoke("SomeMethod");
    }
    catch
    {
    }
}

Am I right in saying that this is asynchronous, in as much as it will return "immediately", rather than wait for the invoke to complete? If this is the case, would the try..catch actually catch any exceptions during the invoke, e.g. if the connection had closed? (And if not, how would I do so?)

If I was to add .Wait(), would this make the code synchronous, so it only returns once the invoke had completed?

Finally, is there any advantage in using await on the invoke, or is this only useful when invoking a server method that has a return value?

Upvotes: 3

Views: 7242

Answers (1)

Wasp
Wasp

Reputation: 3425

Yes, the invocation is asynchronous, therefore your code would not catch any exception apart (most likely) from when you try to invoke a method while being actually disconnected (that check should run synchronously). In case you await the call, then yes your try...catch block would trap errors happening remotely on the server-side method invocation (but there are other things going on there, like the fact that you have to allow detailed errors to go down to the client). Still, in order to catch more general errors related, for example, on failures related to the connection, you should subscribe to the Error event on the connection object, like this:

 hubConnection.Error += ex => Console.WriteLine("Error: {0}", ex.Message);

Check here: http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-net-client#handleerrors

Upvotes: 2

Related Questions