.NET Aspire Azure Blob Storage integration
In this article, you learn how to use the .NET Aspire Azure Blob Storage integration. The Aspire.Azure.Storage.Blobs
library is used to register a BlobServiceClient in the DI container for connecting to Azure Blob Storage. It also enables corresponding health checks, logging and telemetry.
Get started
To get started with the .NET Aspire Azure Blob Storage integration, install the Aspire.Azure.Storage.Blobs NuGet package in the client-consuming project, i.e., the project for the application that uses the Azure Blob Storage client.
dotnet add package Aspire.Azure.Storage.Blobs
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 AddAzureBlobClient extension to register a BlobServiceClient
for use via the dependency injection container.
builder.AddAzureBlobClient("blobs");
You can then retrieve the BlobServiceClient
instance using dependency injection. For example, to retrieve the client from a service:
public class ExampleService(BlobServiceClient 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, register the Azure Blob Storage integration and consume the service using the following methods, such as AddAzureStorage:
var builder = DistributedApplication.CreateBuilder(args);
var blobs = builder.AddAzureStorage("storage")
.AddBlobs("blobs");
var exampleProject = builder.AddProject<Projects.ExampleProject>()
.WithReference(blobs);
The AddBlobs method will read connection information from the AppHost's configuration (for example, from "user secrets") under the ConnectionStrings:blobs
config key. The WithReference method passes that connection information into a connection string named blobs in the ExampleProject
project. In the Program.cs file of ExampleProject
, the connection can be consumed using:
builder.AddAzureBlobClient("blobs");
Configuration
The .NET Aspire Azure Blob Storage integration provides multiple options to configure the BlobServiceClient
based on the requirements and conventions of your project.
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.AddAzureBlobClient
:
builder.AddAzureBlobClient("blobs");
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 AzureStorageBlobsSettings.Credential property to establish a connection. If no credential is configured, the Azure.Identity.DefaultAzureCredential is used.
{
"ConnectionStrings": {
"blobsConnectionName": "https://{account_name}.blob.core.windows.net/"
}
}
Connection string
Alternatively, an Azure Storage connection string can be used.
{
"ConnectionStrings": {
"blobsConnectionName": "AccountName=myaccount;AccountKey=myaccountkey"
}
}
Use configuration providers
The .NET Aspire Azure Blob Storage integration supports Microsoft.Extensions.Configuration. It loads the AzureStorageBlobsSettings and BlobClientOptions from configuration by using the Aspire:Azure:Storage:Blobs
key. Example appsettings.json that configures some of the options:
{
"Aspire": {
"Azure": {
"Storage": {
"Blobs": {
"DisableHealthChecks": true,
"DisableTracing": false,
"ClientOptions": {
"Diagnostics": {
"ApplicationId": "myapp"
}
}
}
}
}
}
}
Use inline delegates
You can also pass the Action<AzureStorageBlobsSettings> configureSettings
delegate to set up some or all the options inline, for example to configure health checks:
builder.AddAzureBlobClient(
"blobs",
static settings => settings.DisableHealthChecks = true);
You can also set up the BlobClientOptions
using Action<IAzureClientBuilder<BlobServiceClient, BlobClientOptions>> configureClientBuilder
delegate, the second parameter of the AddAzureBlobClient
method. For example, to set the first part of user-agent headers for all requests issues by this client:
builder.AddAzureBlobClient(
"blobs",
static configureClientBuilder: 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 Blob Storage integration handles the following:
- Adds the
AzureBlobStorageHealthCheck
health check, which attempts to connect to and query blob storage - 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 Blob Storage integration uses the following log categories:
Azure.Core
Azure.Identity
Tracing
The .NET Aspire Azure Blob Storage integration will emit the following tracing activities using OpenTelemetry:
- "Azure.Storage.Blobs.BlobContainerClient"
Metrics
The .NET Aspire Azure Blob Storage integration currently does not support metrics by default due to limitations with the Azure SDK.
See also
.NET Aspire