A SignalR connection is the client-side object that connects a UI to a hub. Build it with a hub URL, register handlers for server events, and start it when the UI is ready. In an order-tracking page, the connection receives location updates that move a marker on a map. section 4.4.2
For a Blazor page connecting to a separate Aspire-hosted API service:
- Register HTTP client services, inject IHttpMessageHandlerFactory, and pass it to the connection builder. A WithUrl extension assigns a handler from that factory to the connection options, enabling service discovery for a named-service URL such as https://apiservice/locationHub. section 4.4.2
- Build the connection during component initialization. After the first render, initialize the map, register the ReceiveLocationUpdate handler, then call StartAsync. This order lets the handler update the rendered map when coordinates arrive. section 4.4.2
Listing 4.2 Extension class for SignalR service discovery
using Microsoft.AspNetCore.SignalR.Client;
namespace OnlineShop.Web.Extensions;
public static class HubConnectionExtensions
{
public static IHubConnectionBuilder WithUrl(
this IHubConnectionBuilder builder,
string url,
IHttpMessageHandlerFactory clientFactory)
{
return builder.WithUrl(
url, options =>
{
options.HttpMessageHandlerFactory =
_ => clientFactory.CreateHandler();
});
}
}
The WithUrl extension supplies the connection’s HTTP message handler through IHttpMessageHandlerFactory, so the Aspire service name can be resolved. section 4.4.2
Browser chat connection
A JavaScript chat client builds its hub URL from the application base URL, enables automatic reconnect and logging, and reports reconnecting, connected, or disconnected status in the UI. Once started, the same connection opens a StreamAnswer stream; the page appends incoming chunks and can dispose of the subscription when the user presses Stop. section 5.3.1
