microsoft/aspire · error · InvalidOperationException

is not initialized.

Error message

{nameof(BlobServiceClient)} is not initialized.

What it means

In the OnResourceReady handler of RunAsEmulator, blobServiceClient (and queueServiceClient) are captured from the earlier startup event; if either is still null when ResourceReady fires, an InvalidOperationException is thrown. This means the resource reached ready state without the clients being initialized, which should never happen in normal operation.

Solutions

  1. Update Aspire packages and retry; this indicates an internal lifecycle invariant violation
  2. Check logs for earlier failures in the OnBeforeResourceStarted handler that left clients null
  3. File an issue with repro details if the error reproduces on a clean setup
Defensive patterns

Strategy: try-catch

Validate before calling

if (blobServiceClient is null || queueServiceClient is null)
    throw new InvalidOperationException("Emulator clients not initialized; resource start event did not complete.");

Try / catch

try { await onResourceReady(ct); }
catch (InvalidOperationException ex) when (ex.Message.Contains("not initialized")) { logger.LogError(ex, "Emulator client lifecycle invariant violated"); throw; }

Prevention

When it happens

Trigger: ResourceReadyEvent firing before/without OnBeforeResourceCompleted having successfully initialized the clients, e.g. if startup event handling was skipped or failed silently.

Common situations: Unusual startup ordering, event subscription issues, or exceptions swallowed during client initialization followed by a ready signal.

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/5b3d952266aa6413. Report an issue: GitHub.

Appendix: source

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

        builder
            .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";

View on GitHub (pinned to 25830f84bd)