Gå til indhold
Docs Try Aspire
Docs Try

Connect to Azure Web PubSub

Dette indhold er ikke tilgængeligt i dit sprog endnu.

Azure Web PubSub logo

This page describes how consuming apps connect to an Azure Web PubSub resource that’s already modeled in your AppHost. For the AppHost API surface — adding a Web PubSub resource, hubs, event handlers, role assignments, and more — see Azure Web PubSub Hosting integration.

When you reference an Azure Web PubSub resource from your AppHost, Aspire injects the connection information into the consuming app as environment variables. Your app can either read those environment variables directly — the pattern works the same from any language — or, in C#, use the Aspire Azure Web PubSub client integration for automatic dependency injection, health checks, and telemetry.

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

The Azure Web PubSub resource exposes the following connection properties:

Property NameDescription
UriThe HTTPS service endpoint, with the format https://{resource-name}.webpubsub.azure.com

Example:

WEB_PUBSUB_URI: https://webpubsub-abc123.webpubsub.azure.com

Azure Web PubSub clients accept two connection formats:

  • Service endpoint (recommended): Uses the endpoint URL with a managed identity / default credential. This is the format Aspire injects by default.

    https://{account_name}.webpubsub.azure.com
  • Full connection string (includes access key): Combines the Endpoint and AccessKey components. Use this only when managed identity isn’t available.

    Endpoint=https://{account_name}.webpubsub.azure.com;AccessKey={account_key}

Pick the language your consuming app is written in. Each example assumes your AppHost adds an Azure Web PubSub resource named web-pubsub and references it from the consuming app.

For C# apps, the recommended approach is the Aspire Azure Web PubSub client integration. It registers a WebPubSubServiceClient through dependency injection and adds health checks and telemetry automatically. If you’d rather read environment variables directly, see the Read environment variables section at the end of this tab.

Install the 📦 Aspire.Azure.Messaging.WebPubSub NuGet package in the client-consuming project:

.NET CLI — Add Aspire.Azure.Messaging.WebPubSub package
dotnet add package Aspire.Azure.Messaging.WebPubSub

In Program.cs, call AddAzureWebPubSubServiceClient on your IHostApplicationBuilder to register a WebPubSubServiceClient:

C# — Program.cs
builder.AddAzureWebPubSubServiceClient(
connectionName: "web-pubsub",
settings => settings.HubName = "messages");

Resolve the client through dependency injection:

C# — ExampleService.cs
public class ExampleService(WebPubSubServiceClient client)
{
// Use client...
}

To register multiple WebPubSubServiceClient instances for different hubs, use AddKeyedAzureWebPubSubServiceClient:

C# — Program.cs
builder.AddKeyedAzureWebPubSubServiceClient(name: "messages");
builder.AddKeyedAzureWebPubSubServiceClient(name: "commands");

Then resolve each client by key:

C# — ExampleService.cs
public class ExampleService(
[FromKeyedServices("messages")] WebPubSubServiceClient messagesClient,
[FromKeyedServices("commands")] WebPubSubServiceClient commandsClient)
{
// Use clients...
}

The Aspire Azure Web PubSub client integration offers multiple ways to provide configuration.

Connection strings. When using a connection string from the ConnectionStrings configuration section, pass the connection name to AddAzureWebPubSubServiceClient. Two formats are supported:

  • Service endpoint (recommended):

    JSON — appsettings.json
    {
    "ConnectionStrings": {
    "web-pubsub": "https://{account_name}.webpubsub.azure.com"
    }
    }
  • Full connection string with access key:

    JSON — appsettings.json
    {
    "ConnectionStrings": {
    "web-pubsub": "Endpoint=https://{account_name}.webpubsub.azure.com;AccessKey={account_key}"
    }
    }

Configuration providers. The client integration supports Microsoft.Extensions.Configuration. It loads AzureMessagingWebPubSubSettings from appsettings.json using the Aspire:Azure:Messaging:WebPubSub key:

JSON — appsettings.json
{
"Aspire": {
"Azure": {
"Messaging": {
"WebPubSub": {
"HubName": "messages",
"DisableHealthChecks": false
}
}
}
}
}

Inline delegates. Pass an Action<AzureMessagingWebPubSubSettings> to configure settings inline:

C# — Program.cs
builder.AddAzureWebPubSubServiceClient(
"web-pubsub",
settings => settings.DisableHealthChecks = true);

Aspire client integrations enable health checks by default. The Azure Web PubSub client integration registers a health check that verifies the service is reachable. The health check is wired into the /health HTTP endpoint, where all registered health checks must pass before the app is considered ready to accept traffic.

The Aspire Azure Web PubSub client integration automatically configures logging, tracing, and metrics through OpenTelemetry.

Logging categories:

  • Azure
  • Azure.Core
  • Azure.Identity
  • Azure.Messaging.WebPubSub

Tracing activities:

  • Azure.Messaging.WebPubSub.*

Metrics: The Azure Web PubSub integration currently doesn’t support metrics by default due to limitations in the Azure SDK for .NET.

If you prefer not to use the Aspire client integration, you can read the Aspire-injected endpoint URI from the environment and create a WebPubSubServiceClient directly:

C# — Program.cs
using Azure.Messaging.WebPubSub;
using Azure.Identity;
var endpoint = new Uri(Environment.GetEnvironmentVariable("WEB_PUBSUB_URI")!);
var client = new WebPubSubServiceClient(endpoint, "messages", new DefaultAzureCredential());
// Use client...