Pular para o conteúdo
Docs Try Aspire
Docs Try

Set up Azure Queue Storage in the AppHost

Este conteúdo não está disponível em sua língua ainda.

Azure Queue Storage logo

This article is the reference for the Aspire Azure Queue Storage Hosting integration. It enumerates the AppHost APIs — with examples for both AppHost.cs and apphost.ts — that you use to model an Azure Storage account and its queue resources in your AppHost project.

If you’re new to the Azure Queue Storage integration, start with the Get started with Azure Queue Storage integrations guide. For how consuming apps read the connection information this page exposes, see Connect to Azure Queue Storage.

To start building an Aspire app that uses Azure Queue Storage, install the 📦 Aspire.Hosting.Azure.Storage NuGet package:

Terminal
aspire add azure-storage

Learn more about aspire add in the command reference.

Or, choose a manual installation approach:

C# — AppHost.cs
#:package Aspire.Hosting.Azure.Storage@*
XML — AppHost.csproj
<PackageReference Include="Aspire.Hosting.Azure.Storage" Version="*" />

Once you’ve installed the hosting integration in your AppHost project, you can add an Azure Storage resource and attach a queue resource to it:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var queues = builder.AddAzureStorage("storage")
.AddQueues("queues");
builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(queues)
.WaitFor(queues);
// After adding all resources, run the app...
builder.Build().Run();

The preceding code adds an Azure Storage resource named storage, attaches a queue storage resource named queues to it, and passes the queue’s connection information to the consuming project.

For local development, Aspire can run the Azurite emulator in a container to avoid requiring an Azure subscription. Call RunAsEmulator (C#) or runAsEmulator (TypeScript) on the storage resource:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var queues = builder.AddAzureStorage("storage")
.RunAsEmulator()
.AddQueues("queues");
builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(queues)
.WaitFor(queues);
// After adding all resources, run the app...
builder.Build().Run();

When running as an emulator, Aspire pulls the mcr.microsoft.com/azure-storage/azurite container image and starts a local Azurite instance.

By default, the Azurite container exposes the following endpoints:

EndpointContainer portHost port
blob10000dynamic
queue10001dynamic
table10002dynamic

The host port is dynamic by default. To fix the ports, pass a configuration callback to RunAsEmulator (C#) or runAsEmulator (TypeScript):

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var storage = builder.AddAzureStorage("storage")
.RunAsEmulator(azurite =>
{
azurite.WithBlobPort(27000)
.WithQueuePort(27001)
.WithTablePort(27002);
});
// After adding all resources, run the app...
builder.Build().Run();

The resulting port mappings (container:host) are:

EndpointPort mapping
blob10000:27000
queue10001:27001
table10002:27002

Configure Azurite container with persistent lifetime

Section titled “Configure Azurite container with persistent lifetime”

To keep the Azurite container alive across AppHost restarts, call WithLifetime with ContainerLifetime.Persistent:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var storage = builder.AddAzureStorage("storage")
.RunAsEmulator(azurite =>
{
azurite.WithLifetime(ContainerLifetime.Persistent);
});
// After adding all resources, run the app...
builder.Build().Run();

Configure Azurite container with data volume

Section titled “Configure Azurite container with data volume”

To persist Azurite data outside the container lifecycle, add a data volume:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var storage = builder.AddAzureStorage("storage")
.RunAsEmulator(azurite =>
{
azurite.WithDataVolume();
});
// After adding all resources, run the app...
builder.Build().Run();

The data volume is mounted at the /data path in the Azurite container. When a name parameter isn’t provided, the name is formatted as .azurite/{resource name}. For more information on data volumes and details on why they’re preferred over bind mounts, see Docker docs: Volumes.

Configure Azurite container with data bind mount

Section titled “Configure Azurite container with data bind mount”

To use a bind mount instead of a volume, call WithDataBindMount:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var storage = builder.AddAzureStorage("storage")
.RunAsEmulator(azurite =>
{
azurite.WithDataBindMount("../azurite/data");
});
// After adding all resources, run the app...
builder.Build().Run();

Data bind mounts rely on the host machine’s filesystem to persist Azurite data across container restarts. The data bind mount is mounted at the ../azurite/data path on the host machine relative to the AppHost directory. For more information on data bind mounts, see Docker docs: Bind mounts.

Connect to an existing Azure Storage account

Section titled “Connect to an existing Azure Storage account”

You might have an existing Azure Storage account that you want to connect to. Call AsExisting (or asExisting) to annotate the resource as already provisioned:

C# — AppHost.cs
var builder = DistributedApplication.CreateBuilder(args);
var existingStorageName = builder.AddParameter("existingStorageName");
var existingStorageResourceGroup = builder.AddParameter("existingStorageResourceGroup");
var queues = builder.AddAzureStorage("storage")
.AsExisting(existingStorageName, existingStorageResourceGroup)
.AddQueues("queues");
builder.AddProject<Projects.ExampleProject>("apiservice")
.WithReference(queues);
// After adding all resources, run the app...
builder.Build().Run();

Connect to storage resources from Azure Storage Explorer

Section titled “Connect to storage resources from Azure Storage Explorer”

When the Aspire AppHost runs, the storage resources can be accessed by external tools such as the Azure Storage Explorer. If your storage resource is running locally using Azurite, it will automatically be picked up by the Azure Storage Explorer.

To connect to the storage resource from Azure Storage Explorer, follow these steps:

  1. Run the Aspire AppHost.

  2. Open the Azure Storage Explorer.

  3. View the Explorer pane.

  4. Select the Refresh all link to refresh the list of storage accounts.

  5. Expand the Emulator & Attached node.

  6. Expand the Storage Accounts node.

  7. You should see a storage account with your resource’s name as a prefix:

    Azure Storage Explorer: Azurite storage resource discovered.

For more information on using the Azure Storage Explorer, see Get started with Storage Explorer.

For the full reference of Azure Queue Storage connection properties — and how consuming apps in C#, TypeScript, Python, and Go read them — see Connect to Azure Queue Storage.

The Azure Queue Storage hosting integration automatically adds a health check for the queue resource. The health check verifies that the queue service is running and that a connection can be established to it.