Gå til indhold
Docs Try Aspire
Docs Try

Connect to Azure Service Bus

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

Azure Service Bus logo

This page describes how consuming apps connect to an Azure Service Bus resource that’s already modeled in your AppHost. For the AppHost API surface — adding a Service Bus namespace, queues, topics, subscriptions, the emulator, and more — see Azure Service Bus Hosting integration.

When you reference a Service Bus 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 Service Bus client integration for automatic dependency injection, health checks, and telemetry.

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

The Service Bus namespace resource exposes the following connection properties:

Property NameDescription
HostThe hostname of the Service Bus namespace
PortThe port of the Service Bus namespace (emulator only)
EndpointThe namespace endpoint, with the format sb://{namespace}.servicebus.windows.net
FullyQualifiedNamespaceThe fully qualified namespace hostname, e.g. {namespace}.servicebus.windows.net
ConnectionStringEmulator only. Includes SAS key material for the local emulator connection.

The Service Bus queue resource inherits all properties from its parent namespace and adds:

Property NameDescription
QueueNameThe name of the queue

The Service Bus topic resource inherits all properties from its parent namespace and adds:

Property NameDescription
TopicNameThe name of the topic

The Service Bus subscription resource inherits all properties from its parent topic and adds:

Property NameDescription
SubscriptionNameThe name of the subscription

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

For C# apps, the recommended approach is the Aspire Azure Service Bus client integration. It registers a ServiceBusClient through dependency injection and adds health checks and telemetry automatically.

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

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

In Program.cs, call AddAzureServiceBusClient on your IHostApplicationBuilder to register a ServiceBusClient:

C# — Program.cs
builder.AddAzureServiceBusClient(connectionName: "messaging");

Resolve the client through dependency injection:

C# — ExampleService.cs
public class ExampleService(ServiceBusClient client)
{
// Use client to send or receive messages...
}

To register multiple ServiceBusClient instances with different connection names, use AddKeyedAzureServiceBusClient:

C# — Program.cs
builder.AddKeyedAzureServiceBusClient(name: "mainBus");
builder.AddKeyedAzureServiceBusClient(name: "loggingBus");

Then resolve each instance by key:

C# — ExampleService.cs
public class ExampleService(
[FromKeyedServices("mainBus")] ServiceBusClient mainBusClient,
[FromKeyedServices("loggingBus")] ServiceBusClient loggingBusClient)
{
// Use clients...
}

For more information, see .NET dependency injection: Keyed services.

The Aspire Azure Service Bus 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 AddAzureServiceBusClient:

C# — Program.cs
builder.AddAzureServiceBusClient("messaging");

The connection string is resolved from the ConnectionStrings section:

JSON — appsettings.json
{
"ConnectionStrings": {
"messaging": "Endpoint=sb://{namespace}.servicebus.windows.net/;SharedAccessKeyName={keyName};SharedAccessKey={key};"
}
}

Configuration providers. The client integration supports Microsoft.Extensions.Configuration. It loads AzureMessagingServiceBusSettings from appsettings.json (or any other configuration source) by using the Aspire:Azure:Messaging:ServiceBus key:

JSON — appsettings.json
{
"Aspire": {
"Azure": {
"Messaging": {
"ServiceBus": {
"ConnectionString": "Endpoint=sb://{namespace}.servicebus.windows.net/;SharedAccessKeyName={keyName};SharedAccessKey={key};",
"DisableTracing": false
}
}
}
}
}

For the complete configuration schema, see Aspire.Azure.Messaging.ServiceBus/ConfigurationSchema.json.

Named configuration. The integration supports named configuration for multiple instances:

JSON — appsettings.json
{
"Aspire": {
"Azure": {
"Messaging": {
"ServiceBus": {
"bus1": {
"ConnectionString": "Endpoint=sb://namespace1.servicebus.windows.net/;SharedAccessKeyName=keyName;SharedAccessKey=mykey;"
},
"bus2": {
"ConnectionString": "Endpoint=sb://namespace2.servicebus.windows.net/;SharedAccessKeyName=keyName;SharedAccessKey=mykey;"
}
}
}
}
}
}

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

C# — Program.cs
builder.AddAzureServiceBusClient(
"messaging",
static settings => settings.DisableTracing = true);

You can also configure ServiceBusClientOptions using the optional configureClientOptions parameter:

C# — Program.cs
builder.AddAzureServiceBusClient(
"messaging",
configureClientOptions:
clientOptions => clientOptions.Identifier = "myapp");

Aspire client integrations enable health checks by default. The Azure Service Bus client integration verifies that the Service Bus 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 Service Bus client integration automatically configures logging, tracing, and metrics through OpenTelemetry.

Logging categories:

  • Azure.Core
  • Azure.Identity
  • Azure-Messaging-ServiceBus

Tracing activities:

  • Message
  • ServiceBusSender.Send
  • ServiceBusSender.Schedule
  • ServiceBusSender.Cancel
  • ServiceBusReceiver.Receive
  • ServiceBusReceiver.ReceiveDeferred
  • ServiceBusReceiver.Peek
  • ServiceBusReceiver.Abandon
  • ServiceBusReceiver.Complete
  • ServiceBusReceiver.DeadLetter
  • ServiceBusReceiver.Defer
  • ServiceBusReceiver.RenewMessageLock
  • ServiceBusSessionReceiver.RenewSessionLock
  • ServiceBusSessionReceiver.GetSessionState
  • ServiceBusSessionReceiver.SetSessionState
  • ServiceBusProcessor.ProcessMessage
  • ServiceBusSessionProcessor.ProcessSessionMessage
  • ServiceBusRuleManager.CreateRule
  • ServiceBusRuleManager.DeleteRule
  • ServiceBusRuleManager.GetRules

Metrics are not currently emitted by default due to limitations in the Azure SDK.