Перейти к содержимому
Docs Try Aspire
Docs Try

Connect to Azure Queue Storage

Это содержимое пока не доступно на вашем языке.

Azure Queue Storage logo

This page describes how consuming apps connect to an Azure Queue Storage resource that’s already modeled in your AppHost. For the AppHost API surface — adding an Azure Storage account, Azurite emulator configuration, data volumes, and more — see Azure Queue Storage Hosting integration.

When you reference an Azure Queue Storage 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 Queue Storage client integration for automatic dependency injection, health checks, and telemetry.

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

The Queue Storage resource exposes the following connection properties:

Property NameDescription
ConnectionStringEmulator only. The full connection string for the Azurite emulator
UriThe queue service endpoint, with the format https://{account}.queue.core.windows.net/

Example values (emulator):

ConnectionString: DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8...;QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1
Uri: http://127.0.0.1:10001/devstoreaccount1

The Queue resource inherits all properties from its parent Queue Storage resource and adds:

Property NameDescription
QueueNameThe name of the specific queue

For example, if you reference a Queue Storage resource named queues in your AppHost project, the following environment variables are available in the consuming project:

  • QUEUES_CONNECTIONSTRING (emulator only)
  • QUEUES_URI
  • QUEUES_QUEUENAME

Pick the language your consuming app is written in. Each example assumes your AppHost adds a queue resource named queues and references it from the consuming app.

For C# apps, the recommended approach is the Aspire Azure Queue Storage client integration. It registers a QueueServiceClient 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.Storage.Queues NuGet package in the client-consuming project:

.NET CLI — Add Aspire.Azure.Storage.Queues package
dotnet add package Aspire.Azure.Storage.Queues

In Program.cs, call AddAzureQueueServiceClient on your IHostApplicationBuilder to register a QueueServiceClient:

C# — Program.cs
builder.AddAzureQueueServiceClient(connectionName: "queues");

Resolve the client through dependency injection:

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

To register multiple QueueServiceClient instances with different connection names, use AddKeyedAzureQueueServiceClient:

C# — Program.cs
builder.AddKeyedAzureQueueServiceClient(name: "orders");
builder.AddKeyedAzureQueueServiceClient(name: "notifications");

Then resolve each instance by key:

C# — ExampleService.cs
public class ExampleService(
[FromKeyedServices("orders")] QueueServiceClient ordersClient,
[FromKeyedServices("notifications")] QueueServiceClient notificationsClient)
{
// Use clients...
}

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

The Aspire Azure Queue Storage 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 AddAzureQueueServiceClient:

C# — Program.cs
builder.AddAzureQueueServiceClient("queues");

Two formats are supported in the ConnectionStrings section:

JSON — appsettings.json (service URI)
{
"ConnectionStrings": {
"queues": "https://{account_name}.queue.core.windows.net/"
}
}
JSON — appsettings.json (connection string)
{
"ConnectionStrings": {
"queues": "AccountName=myaccount;AccountKey=myaccountkey"
}
}

The service URI approach is recommended for production because it works with default Azure credentials.

Configuration providers. The client integration supports Microsoft.Extensions.Configuration. It loads AzureStorageQueuesSettings and QueueClientOptions from configuration using the Aspire:Azure:Storage:Queues key:

JSON — appsettings.json
{
"Aspire": {
"Azure": {
"Storage": {
"Queues": {
"DisableHealthChecks": false,
"DisableTracing": false,
"ClientOptions": {
"Diagnostics": {
"ApplicationId": "myapp"
}
}
}
}
}
}
}

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

C# — Program.cs
builder.AddAzureQueueServiceClient(
"queues",
settings => settings.DisableHealthChecks = true);

You can also configure QueueClientOptions:

C# — Program.cs
builder.AddAzureQueueServiceClient(
"queues",
configureClientBuilder: clientBuilder =>
clientBuilder.ConfigureOptions(
options => options.Diagnostics.ApplicationId = "myapp"));

Aspire client integrations enable health checks by default. The Azure Queue Storage client integration:

  • Adds a health check when DisableHealthChecks is false, which verifies that a connection can be established to the queue service.
  • Integrates with the /health HTTP endpoint, where all registered health checks must pass before the app is considered ready to accept traffic.

The Aspire Azure Queue Storage client integration automatically configures logging and tracing through OpenTelemetry.

Logging categories:

  • Azure.Core
  • Azure.Identity

Tracing activities:

  • Azure.Storage.Queues.QueueClient

Metrics: The Azure Queue Storage integration currently doesn’t support metrics by default due to limitations in the Azure SDK.

If you prefer not to use the Aspire client integration, you can read the Aspire-injected service URI from the environment and create a QueueServiceClient directly using 📦 Azure.Storage.Queues:

C# — Program.cs
using Azure.Identity;
using Azure.Storage.Queues;
// Use connection string if available (emulator), otherwise use service URI with default credential
var connectionString = Environment.GetEnvironmentVariable("QUEUES_CONNECTIONSTRING");
QueueServiceClient client;
if (!string.IsNullOrEmpty(connectionString))
{
client = new QueueServiceClient(connectionString);
}
else
{
var serviceUri = new Uri(Environment.GetEnvironmentVariable("QUEUES_URI")!);
client = new QueueServiceClient(serviceUri, new DefaultAzureCredential());
}
// Use client...