Frank Monroe
Frank Monroe

Reputation: 1611

How to check success of HubConnection.StartAsync in SignalR for ASP.NET Core 2.0?

Using SignalR for ASP.NET Core 2.0, Client side (1.0.0-alpha2-final), C# VS2017.

To connect the code is: await hubConnection.StartAsync(); since this is an await for a void how can we test if the client did manage to connect ? In my tests I have a breakpoint on the server side in public override async Task OnConnectedAsync() but that breakpoint is not always hit therefore I know that sometimes the client does not connect. Or perhaps it did connect but OnConnectedAsync() was not triggered?

How can the client knows it succeeded in establishing the connection?

Thanks

Edit: More info. It does fire OnConnectedAsync() when using localhost (debugging in VS); it does not fire OnConnectedAsync() when connecting through ngrok. Maybe this is something to do with proxy/CORS, I am not familiar with.

Edit: It appears that the Client does indeed connect because if I change the URL to a non existing one it gives a 404. So it appears that the problem is that OnConnectedAsync() does not fire when the client connects through ngrok but does fire when connected directly to localhost.

Upvotes: 4

Views: 4356

Answers (1)

Pawel
Pawel

Reputation: 31610

StartAsync is not a void method but returns a Task. If the client does not connect StartAsync throws (returns a faulted task) otherwise the async call will complete without an error (returns a completed task) after the client is successfully connected.

Upvotes: 2

Related Questions