In this lesson, you will learn how to weave SignalR into the ASP.NET Core request pipeline using endpoint routing, transforming your application from a standard request-response model into a bi-directional communication hub.
Integrating SignalR into the Pipeline
By the time a request reaches your server, the middleware pipeline has already processed authentication and routing concerns. SignalR, being a specialized real-time layer, needs to be explicitly mapped within the MapEndpoints (or UseEndpoints in legacy configurations) block. This ensures that the middleware recognizes specific URL patterns as entry points for your Hubs rather than standard MVC controllers or static files.
To configure this, you define the mapping within your Program.cs. Think of this as defining a "listening station" for your real-time traffic:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSignalR();
var app = builder.Build();
app.MapHub<ChatHub>("/chathub");
app.Run();
The Order of Execution
Because SignalR uses its own persistent connection protocol, its placement within the middleware pipeline is critical. While MapHub is typically used at the end of the routing definition, it is subject to the same cross-cutting concerns as your other endpoints. If you have defined custom middleware—such as a logger or a rate-limiter—these will intercept the initial handshake request for the SignalR connection.
When a client initiates a connection, the pipeline first performs an HTTP request (the "Handshake"). If this succeeds, the middleware upgrades the request to a persistent WebSocket, Server-Sent Events, or Long Polling connection. Once the pipeline has successfully "handed off" the connection to the Hub, the middleware's role for that specific request changes; the Hub takes ownership of the lifecycle management, allowing for high-frequency, low-latency communication that bypasses standard request-response overhead.
By mapping your Hubs at the root level of your routing configuration, you ensure that the SignalR middleware has the highest priority for handling incoming WebSocket upgrade requests while remaining compatible with your existing security and logging infrastructure.
Key Takeaway: Configuring the SignalR middleware is accomplished by mapping Hub endpoints directly to the request pipeline, allowing the server to intercept handshake requests and promote them to persistent real-time connections.