Pular para o conteúdo
Docs Try Aspire
Docs Try

Connect to Azure SignalR Service

Este conteúdo não está disponível em sua língua ainda.

Azure SignalR Service icon

This page describes how consuming apps connect to an Azure SignalR Service resource that’s already modeled in your AppHost. For the AppHost API surface — adding a SignalR resource, service modes, emulator, and Bicep customization — see Azure SignalR Service hosting integration.

Azure SignalR Service acts as a proxy between hub servers (where Hub or Hub<T> types are hosted) and clients (browser or mobile apps using the SignalR client). The architecture differs from a direct database connection: the server never receives connections from clients directly — Azure SignalR Service routes all traffic.

When you reference an Azure SignalR Service resource from a consuming app, Aspire injects the following connection properties:

Property NameDescription
UriThe endpoint URI for the SignalR service, with the format https://{host} in Azure (typically https://<resource-name>.service.signalr.net) or the emulator endpoint when running locally

Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the Uri property of a resource called signalr becomes SIGNALR_URI.

Additionally, Aspire sets ConnectionStrings__signalr (which maps to ConnectionStrings:signalr in .NET configuration) for use by the AddNamedAzureSignalR extension method in Default mode.

Example values:

SIGNALR_URI: https://my-signalr.service.signalr.net
ConnectionStrings__signalr: Endpoint=https://my-signalr.service.signalr.net;AuthType=azure.msitoken;...

When using the emulator locally, the connection string includes an AccessKey:

ConnectionStrings__signalr: Endpoint=http://localhost:8888;AccessKey=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789;Version=1.0;

Pick the language your consuming app is written in. Each example assumes your AppHost adds an Azure SignalR Service resource named signalr and references it from the consuming app.

The recommended approach for C# hub server apps is the Microsoft.Azure.SignalR package. It integrates Azure SignalR Service directly into ASP.NET Core’s SignalR infrastructure and reads configuration injected by Aspire.

In Default mode, your hub server project registers its hubs normally with Azure SignalR Service acting as the transport layer. Install the 📦 Microsoft.Azure.SignalR NuGet package:

.NET CLI — Add Microsoft.Azure.SignalR package
dotnet add package Microsoft.Azure.SignalR

In Program.cs, chain AddNamedAzureSignalR onto AddSignalR:

C# — Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSignalR()
.AddNamedAzureSignalR("signalr");
var app = builder.Build();
app.MapHub<ChatHub>("/chat");
app.Run();

The AddNamedAzureSignalR method reads the connection string from ConnectionStrings:signalr, which Aspire injects as ConnectionStrings__signalr. The connection name must match the resource name used in your AppHost.

In Serverless mode, there is no hub server. Instead, apps communicate with Azure SignalR Service through the Management SDK. Install the 📦 Microsoft.Azure.SignalR.Management NuGet package:

.NET CLI — Add Microsoft.Azure.SignalR.Management package
dotnet add package Microsoft.Azure.SignalR.Management

Register the ServiceManager using the Aspire-injected connection string and expose a /negotiate endpoint:

C# — Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton(sp =>
{
return new ServiceManagerBuilder()
.WithOptions(options =>
{
options.ConnectionString = builder.Configuration.GetConnectionString("signalr");
})
.BuildServiceManager();
});
var app = builder.Build();
app.MapPost("/negotiate", async (string? userId, ServiceManager sm, CancellationToken token) =>
{
var context = await sm.CreateHubContextAsync("messages", token);
var negotiateResponse = await context.NegotiateAsync(new NegotiationOptions
{
UserId = userId
}, token);
return Results.Json(negotiateResponse, new JsonSerializerOptions(JsonSerializerDefaults.Web)
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull
});
});
app.Run();

The /negotiate endpoint establishes a connection between the connecting client and the Azure SignalR Service. The ServiceHubContext is used to broadcast messages and manage connections to clients.

For more information, see Use Azure SignalR Management SDK.

To read the endpoint URI directly from the Aspire-injected environment variable:

C# — Program.cs
var endpoint = Environment.GetEnvironmentVariable("SIGNALR_URI");
// Use endpoint to configure the SignalR Service connection manually