Introduction
In this article, we'll learn how to use Dynamic Hub in ASPNET Core SignalR. The newly written SignalR allows you to write a dynamic type of T Hub. The benefit of using dynamic hub is that the method will resolve to the runtime so that you can register any method at runtime and call it.
This article is part of series of posts on SignalR using ASPNET Core. You can read:
- Overview of New stack SIgnalR on ASPNET Core here
- Getting Started With SignalR Using ASP.NET Core And Angular 5 here
- Working with Azure service
- Working with Xamarin
- Working with streaming data here
This article demonstrates the following tasks:
- Creating Dynamic Hub
- Creating T of Hub
- Consuming the Dynamic and T of Hub by the client
- Demo
The source code is available at GitHub.
Before reading this article, I would highly recommend you to go through the other articles of the series which are mentioned above.
Creating Dynamic Hub
This is a base class for SignalR hubs that uses dynamic keyword to represent client invocations. Basically, the idea behind Dynamic Hub is hidden in the dynamic keyword. DynamicHub is inherited from Hub class. This is an abstract class and it is implementing the DynamicHubClients which contains all the dynamic type properties.
From MSDN
"The dynamic type enables the operations in which it occurs to bypass compile-time type checking. Instead, these operations are resolved at runtime. The dynamic type simplifies access to COM APIs such as the Office Automation APIs, and also to dynamic APIs such as IronPython libraries, and to the HTML Document Object Model (DOM)."
A Hub contains the following properties.
- Clients : A clients caller abstraction for a hub, where you can push message to all connected clients
- Context : A context abstraction for accessing information about the hub caller connection. It contains the connection and claims principle information.
- Groups : A manager abstraction for adding and removing connections from groups. Ease of managing clients grouping.
- Events : It provide the event notification on the client connected or disconnected so that you can override these events and add your custom login.
Dynamic Hub class
- //https://github.com/aspnet/SignalR/blob/dev/src/Microsoft.AspNetCore.SignalR.Core/DynamicHub.cs
- public abstract class DynamicHub : Hub
- {
- private DynamicHubClients _clients;
- /// <summary>
- /// Gets or sets an object that can be used to invoke methods on the clients connected to this hub.
- /// </summary>
- public new DynamicHubClients Clients
- {
- get
- {
- if (_clients == null)
- {
- _clients = new DynamicHubClients(base.Clients);
- }
- return _clients;
- }
- set => _clients = value;
- }
- }
DynamicHubClients Class

Dave EvansPosted May 2, 2019, 3:32 AM
Not sure if I missed something, but the outline for the article states that it will include 'Consuming the Dynamic and T of Hub by the client', but I don't see this anywhere in the article.