microsoft/aspire · error · InvalidOperationException

is not initialized.

Error message

{nameof(QueueServiceClient)} is not initialized.

What it means

RunAsEmulator wires an OnResourceReady callback that creates the blob containers you declared. It asserts the lazily-captured QueueServiceClient was initialized from the resolved connection string before use; if the connection string never resolved, the client is still null and this InvalidOperationException is thrown.

Solutions

  1. Use the stock AddAzureStorage + RunAsEmulator + AddQueues flow so the queue client factory is registered
  2. Verify the storage resource's connection string resolution callback supplies a non-null value before ResourceReady
  3. Update Aspire.Hosting.Azure.Storage to the latest patch to pick up initialization fixes

Example fix

// before: custom init of only blob client
var blob = new BlobServiceClient(conn);
// after: initialize both clients the same way
var blob = new BlobServiceClient(conn);
var queue = new QueueServiceClient(conn);
Defensive patterns

Strategy: validation

Validate before calling

if (storageResource.Resource.IsEmulator && storageResource.Resource.BlobContainers.Any())
{
    // Ensure the stock RunAsEmulator pipeline registered the queue client
}

Prevention

When it happens

Trigger: Calling AddQueues() on an AzureStorageEmulatorResource then starting the app: the ResourceReadyEvent fires, blob containers are created, and the `_ = queueServiceClient ?? throw ...` guard fails because the queued client factory (or connection string resolution) never ran or produced null.

Common situations: Customized emulator setup where the storage connection string callback returns null; package version mismatches in the hosting internals; subclassed/modified RunAsEmulator wiring that initializes only the blob client.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/1d52f83d501e6dfd. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting.Azure.Storage/AzureStorageExtensions.cs:230

            .OnBeforeResourceStarted(async (storage, @event, ct) =>
            {
                // The BlobServiceClient and QueueServiceClient are created before the health check is run.
                // We can't use ConnectionStringAvailableEvent here because the resource doesn't have a connection string, so
                // we use BeforeResourceStartedEvent

                var blobConnectionString = await builder.Resource.GetBlobConnectionString().GetValueAsync(ct).ConfigureAwait(false) ?? throw new DistributedApplicationException($"{nameof(ConnectionStringAvailableEvent)} was published for the '{builder.Resource.Name}' resource but the connection string was null.");
                blobServiceClient = CreateBlobServiceClient(blobConnectionString);

                var queueConnectionString = await builder.Resource.GetQueueConnectionString().GetValueAsync(ct).ConfigureAwait(false) ?? throw new DistributedApplicationException($"{nameof(ConnectionStringAvailableEvent)} was published for the '{builder.Resource.Name}' resource but the connection string was null.");
                queueServiceClient = CreateQueueServiceClient(queueConnectionString);
            })
            .OnResourceReady(async (storage, @event, ct) =>
            {
                // The ResourceReadyEvent of a resource is triggered after its health check (AddAzureBlobStorage) is healthy.
                // This means we can safely use this event to create the blob containers.

                _ = blobServiceClient ?? throw new InvalidOperationException($"{nameof(BlobServiceClient)} is not initialized.");
                _ = queueServiceClient ?? throw new InvalidOperationException($"{nameof(QueueServiceClient)} is not initialized.");

                foreach (var container in builder.Resource.BlobContainers)
                {
                    var blobContainerClient = blobServiceClient.GetBlobContainerClient(container.BlobContainerName);
                    await blobContainerClient.CreateIfNotExistsAsync(cancellationToken: ct).ConfigureAwait(false);
                }

                foreach (var queue in builder.Resource.Queues)
                {
                    var queueClient = queueServiceClient.GetQueueClient(queue.QueueName);
                    await queueClient.CreateIfNotExistsAsync(cancellationToken: ct).ConfigureAwait(false);
                }
            });

        // Add the "Storage" resource health check. There will be separate health checks for the nested child resources.
        var healthCheckKey = $"{builder.Resource.Name}_check";

        builder.ApplicationBuilder.Services.AddHealthChecks().AddAzureBlobStorage(sp =>

View on GitHub (pinned to 25830f84bd)