Editar

Compartir a través de


.NET Aspire Azure Queue Storage integration

In this article, you learn how to use the .NET Aspire Azure Queue Storage integration. The Aspire.Azure.Storage.Queues library is used to register a QueueServiceClient in the DI container for connecting to Azure Queue Storage. It also enables corresponding health checks, logging and telemetry.

Get started

To get started with the .NET Aspire Azure Queue Storage integration, install the 📦 Aspire.Azure.Storage.Queues NuGet package in the client-consuming project, i.e., the project for the application that uses the Azure Queue Storage client.

dotnet add package Aspire.Azure.Storage.Queues

For more information, see dotnet add package or Manage package dependencies in .NET applications.

Example usage

In the Program.cs file of your client-consuming project, call the AddAzureQueueClient extension to register a QueueServiceClient for use via the dependency injection container.

builder.AddAzureQueueClient("queue");

You can then retrieve the QueueServiceClient instance using dependency injection. For example, to retrieve the client from an example service:

public class ExampleService(QueueServiceClient client)
{
    // Use client...
}

App host usage

To add Azure Storage hosting support to your IDistributedApplicationBuilder, install the 📦 Aspire.Hosting.Azure.Storage NuGet package in the app host project.

dotnet add package Aspire.Hosting.Azure.Storage

In your app host project, add a Storage Queue connection and consume the connection using the following methods, such as AddAzureStorage:

var builder = DistributedApplication.CreateBuilder(args);

var queues = builder.AddAzureStorage("storage")
                    .AddQueues("queues");

var exampleProject = builder.AddProject<Projects.ExampleProject>()
                            .WithReference(queues);

The AddQueues method will read connection information from the AppHost's configuration (for example, from "user secrets") under the ConnectionStrings:queue config key. The WithReference method passes that connection information into a connection string named queue in the ExampleProject project. In the Program.cs file of ExampleProject, the connection can be consumed using:

builder.AddAzureQueueClient("queue");

Use a connection string

When using a connection string from the ConnectionStrings configuration section, you can provide the name of the connection string when calling builder.AddAzureQueueClient:

builder.AddAzureQueueClient("queueConnectionName");

And then the connection string will be retrieved from the ConnectionStrings configuration section. Two connection formats are supported:

Service URI

The recommended approach is to use a ServiceUri, which works with the AzureStorageQueuesSettings.Credential property to establish a connection. If no credential is configured, the Azure.Identity.DefaultAzureCredential is used.

{
  "ConnectionStrings": {
    "queueConnectionName": "https://{account_name}.queue.core.windows.net/"
  }
}

Connection string

Alternatively, an Azure Storage connection string can be used.

{
  "ConnectionStrings": {
    "queueConnectionName": "AccountName=myaccount;AccountKey=myaccountkey"
  }
}

Configuration

The .NET Aspire Azure Queue Storage integration provides multiple options to configure the QueueServiceClient based on the requirements and conventions of your project.

Use configuration providers

The .NET Aspire Azure Queue Storage integration supports Microsoft.Extensions.Configuration. It loads the AzureStorageQueuesSettings and QueueClientOptions from configuration by using the Aspire:Azure:Storage:Queues key. Example appsettings.json that configures some of the options:

{
  "Aspire": {
    "Azure": {
      "Storage": {
        "Queues": {
          "DisableHealthChecks": true,
          "DisableTracing": false,
          "ClientOptions": {
            "Diagnostics": {
              "ApplicationId": "myapp"
            }
          }
        }
      }
    }
  }
}

Use inline delegates

You can also pass the Action<AzureStorageQueuesSettings> configureSettings delegate to set up some or all the options inline, for example to disable the health check:

builder.AddAzureQueueClient(
    "queue",
    static settings => settings.DisableHealthChecks  = true);

You can also set up the QueueClientOptions using Action<IAzureClientBuilder<QueueServiceClient, QueueClientOptions>> configureClientBuilder delegate, the second parameter of the AddAzureQueueClient method. For example, to set the first part of user-agent headers for all requests issues by this client:

builder.AddAzureQueueClient(
    "queue",
    configureClientBuilder:
        static clientBuilder => clientBuilder.ConfigureOptions(
            static options =>
                options.Diagnostics.ApplicationId = "myapp"));

Health checks

By default, .NET Aspire integrations enable health checks for all services. For more information, see .NET Aspire integrations overview.

The .NET Aspire Azure Queue Storage integration handles the following:

  • Adds the AzureQueueStorageHealthCheck health check, which attempts to connect to and query the storage queue
  • Integrates with the /health HTTP endpoint, which specifies all registered health checks must pass for app to be considered ready to accept traffic

Observability and telemetry

.NET Aspire integrations automatically set up Logging, Tracing, and Metrics configurations, which are sometimes known as the pillars of observability. For more information about integration observability and telemetry, see .NET Aspire integrations overview. Depending on the backing service, some integrations may only support some of these features. For example, some integrations support logging and tracing, but not metrics. Telemetry features can also be disabled using the techniques presented in the Configuration section.

Logging

The .NET Aspire Azure Queue Storage integration uses the following log categories:

  • Azure.Core
  • Azure.Identity

Tracing

The .NET Aspire Azure Queue Storage integration will emit the following tracing activities using OpenTelemetry:

  • "Azure.Storage.Queues.QueueClient"

Metrics

The .NET Aspire Azure Queue Storage integration currently does not support metrics by default due to limitations with the Azure SDK.

See also