question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[SignalR] (Hub)Exception in OnConnectedAsync sporadically not propagated to .NetClient

See original GitHub issue

Describe the bug

I want to conditionally block some .NET-Client from connecting with my Hub in the OnConnectedAsync overide method by throwing an (hub)exception so that in the client-code the exceptional state gets raised too. Sporadically the (hub)exception does not get propagated to the client.

To Reproduce

Steps to reproduce the behavior:

  1. Using this version of ASP.NET Core ‘2.2.0’, Microsoft.AspNetCore.SignalR.Client ‘1.1.0’
  2. Using this code:

Server/Hub

namespace Server
{
    public class ChatHub : Hub
    {
        public Task SendMessage(string user, string message)
        {
            return Clients.All.SendAsync("ReceiveMessage", user, message);
        }

        public override async Task OnConnectedAsync()
        {
            await base.OnConnectedAsync();
            throw new HubException("Denied");
        }
    }
}

Client

namespace Client
{
    class Program
    {
        static async Task Main(string[] args)
        {
            try
            {
                HubConnection connection = new HubConnectionBuilder()
                .WithUrl("http://localhost:5000/ChatHub")
                .Build();

                await connection.StartAsync();
                await connection.InvokeAsync("SendMessage", "Me", "Hello");
            }
            catch (HubException hubEx)
            {
                Console.WriteLine(hubEx.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
  1. Run the server and then the client (in my case an console app) multiple times (one at a time).
  2. See that the exception is not raised on the second try
  3. After server-restart the (hub)exception is working again (one more time)

Expected behavior

(Hub)Exception gets propagated to client properly every time.

Screenshot

image

Additional context

.NET Core SDK Version: 2.2.106 Commit: aa79b139a8

Windows 7 - x64

Host (useful for support): Version: 2.2.4 Commit: f95848e524

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
BrennanConroycommented, Jul 12, 2019
  1. Crash?

The server will wait for StartAsync to finish before starting to process invokes from the client.

If you really want to wait for some mapping to finish before doing an invoke on the client you could set up a client handler that StartAsync calls.

var tcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
connection.On("Finished", () =>
{
    tcs.TrySetResult(null);
});
connection.Closed += (ex) =>
{
    tcs.TrySetCanceled();
};
await connection.StartAsync();
await tcs.Task;
await connection.InvokeAsync(...);
public class ChatHub : Hub
{
    public override async Task OnConnectedAsync()
    {
        // mapping etc.
        await Clients.Client(Context.ConnectionId).SendAsync("Finished");
    }
}
0reactions
ddwebercommented, Aug 5, 2019

Thanks for the suggestion. I´ ll give it a try.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# .NET Core SignalR exception when client calls a ...
Based on the way that you're capturing the user IDs, it looks like you are trying to treat the SignalR hub as a...
Read more >
Use hubs in ASP.NET Core SignalR
The SignalR Hubs API enables connected clients to call methods on the ... must be propagated to the client, use the HubException class....
Read more >
ASP.NET SignalR Hubs API Guide - Server (C#)
This document provides an introduction to programming the server side of the ASP.NET SignalR Hubs API for SignalR version 2, ...
Read more >
Build Real-time Applications with ASP.NET Core SignalR
It no longer allows a single client to connect to different server-side instances between requests. This means that sticky sessions are required ...
Read more >
Managing SignalR ConnectionIds (or why you shouldn't)
For C# Advent 2020, I want to talk about SignalR ConnectionIds and the ... do not know how to effectively manage connections to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found