[SignalR] (Hub)Exception in OnConnectedAsync sporadically not propagated to .NetClient
See original GitHub issueDescribe 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:
- Using this version of ASP.NET Core ‘2.2.0’, Microsoft.AspNetCore.SignalR.Client ‘1.1.0’
- 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);
}
}
}
}
- Run the server and then the client (in my case an console app) multiple times (one at a time).
- See that the exception is not raised on the second try
- After server-restart the (hub)exception is working again (one more time)
Expected behavior
(Hub)Exception gets propagated to client properly every time.
Screenshot

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:
- Created 4 years ago
- Comments:7 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

The server will wait for
StartAsyncto 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
StartAsynccalls.Thanks for the suggestion. I´ ll give it a try.