I have a Silverlight 5 application accessing a server using SignalR. Most of the times this works fines but now and then when running in debug I get an error during negotiation. I’m thinking that this may be because my local ISS installation runs out of connections.
Anyway, when this occurs, the Unhandled Exception handler in my application is called. I want to change this, so that I can handle the exception local to the initialization of SignalR. I don’t want the negotiation error to propagate to my central error handler.
I have a piece of code which looks as follows:
var uri = Application.Current.Host.Source.GetComponents(UriComponents.Scheme | UriComponents.HostAndPort, UriFormat.Unescaped);
_hubConnection = new HubConnection(uri);
var hub = _hubConnection.CreateProxy("stuffhub");
hub.On<Stuff>("Stuff", message => DoStuff());
var transport = new LongPollingTransport();
_hubConnection.Start(transport);
This code executes fine most of the time, but some times the following exception is handed to my central unhandled excpetion handler a few seconds after the code above has executed.
Message: Exception(s) occurred : .
[ Exception(s) occurred : .
[ Exception(s) occurred : .
[ Exception(s) occurred : .
[ System.InvalidOperationException: Server negotiation failed.
vid SignalR.Client.Transports.HttpBasedTransport.<GetNegotiationResponse>b__0(IResponse response)
vid SignalR.TaskAsyncHelper.FromMethod[T1,TResult](Func`2 func, T1 arg) ]
]
]
]
The StackTrace of the ApplicationUnhandledExceptionEventArgs is:
at System.Threading.Tasks.Task.Finalize()
Like Jon Skeet said, you need to handle the exception from the task returned from Start().