Tracking Group Names In ASP.NET Core SignalR RRS feed

  • Question

  • User2060888240 posted


    I am learning ASP.NET Core and SignalR by making a basic chat room app. After a user logs in, they will be shown a list of available chat rooms to join and also have the option of creating their own new chat room. Correct me if I'm wrong, but it seems as though SignalR groups will meet the functionality requirements of a chat room. However, I am struggling to figure out how to track the list of existing groups so users can see the list of chat rooms availalbe. 

    My google search led to me this SO post which states that the SignalR API does not provide a way to access the list of groups, but that you can create a singleton to track the list of groups yourself:


    The post then links to the following documentation on how to manage groups from outside the hub class, however, this is documentation on how to accomplish this in ASP.NET SignalR, not ASP.NET Core SignalR:


    My next step was to try to figure out how to manage groups from outside the hub class via the hub context in ASP.NET Core SignalR, which led me to this documentation:


    Now this is where I'm stuck. This documentation explains how to 1) Allow for a controller to accept dependency injection of an IHubContext, 2) How to call methods on the IHubContext once it is injected into another class, and 3) How to get an instance of the hub context via middleware:

    app.Use(next => (context) =>
        var hubContext = (IHubContext<MyHub>)context

    What would I put in the //... in the example above to inject the hubContext into a globally accessible singleton similar to the one in the example from the ASP.NET SignalR documentation listed above? Everything I tried resulted in the error:

    Cannot convert lambda expression to intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type

    I'm fairly new to C#, but by the looks of this error, this middleware block needs to have a delegate return type, so specifically, my question is: 1) How do I inject my hub context into a globally accessible singleton in this block and B) How do I return a delegate return type. Or let me know if there's a better way to go about accomplishing my goal.

    Thanks in advance,


    Friday, August 10, 2018 2:13 AM


  • User2060888240 posted

    I was able to figure this out and can explain what I did for anyone in the future who runs into similar confusion. Essentially, I didn't understand how the built-in dependency injection service worked in ASP.NET Core. I had already created my IGroupSingleton interface and GroupSingleton class to track the SignalR groups, then all I had to do was add the following line into the ConfigureServcies method in the Startup class:

    services.AddSingleton<IGroupSingleton, GroupSingleton>();

    ASP.NET Core then automatically creates and manages an instance of my GroupSingleton and automatically injects the IHubContext<ChatHub> that it is dependent on, given that there is an instance of the ChatHub class that is already registered with the app. I can then keep track of groups inside my GroupSingleton class and also send SignalR notifications from the GroupSingleton to keep the connected clients notified of available groups.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, August 12, 2018 11:52 PM