{"record":{"id":"5b3d952266aa6413","repo":"microsoft/aspire","slug":"nameof-blobserviceclient-is-not-initialized","errorCode":null,"errorMessage":"{nameof(BlobServiceClient)} is not initialized.","messagePattern":"(.+?) is not initialized\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.Storage/AzureStorageExtensions.cs","lineNumber":229,"sourceCode":"        builder\n            .OnBeforeResourceStarted(async (storage, @event, ct) =>\n            {\n                // The BlobServiceClient and QueueServiceClient are created before the health check is run.\n                // We can't use ConnectionStringAvailableEvent here because the resource doesn't have a connection string, so\n                // we use BeforeResourceStartedEvent\n\n                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.\");\n                blobServiceClient = CreateBlobServiceClient(blobConnectionString);\n\n                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.\");\n                queueServiceClient = CreateQueueServiceClient(queueConnectionString);\n            })\n            .OnResourceReady(async (storage, @event, ct) =>\n            {\n                // The ResourceReadyEvent of a resource is triggered after its health check (AddAzureBlobStorage) is healthy.\n                // This means we can safely use this event to create the blob containers.\n\n                _ = blobServiceClient ?? throw new InvalidOperationException($\"{nameof(BlobServiceClient)} is not initialized.\");\n                _ = queueServiceClient ?? throw new InvalidOperationException($\"{nameof(QueueServiceClient)} is not initialized.\");\n\n                foreach (var container in builder.Resource.BlobContainers)\n                {\n                    var blobContainerClient = blobServiceClient.GetBlobContainerClient(container.BlobContainerName);\n                    await blobContainerClient.CreateIfNotExistsAsync(cancellationToken: ct).ConfigureAwait(false);\n                }\n\n                foreach (var queue in builder.Resource.Queues)\n                {\n                    var queueClient = queueServiceClient.GetQueueClient(queue.QueueName);\n                    await queueClient.CreateIfNotExistsAsync(cancellationToken: ct).ConfigureAwait(false);\n                }\n            });\n\n        // Add the \"Storage\" resource health check. There will be separate health checks for the nested child resources.\n        var healthCheckKey = $\"{builder.Resource.Name}_check\";\n","sourceCodeStart":211,"sourceCodeEnd":247,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.Storage/AzureStorageExtensions.cs#L211-L247","documentation":"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.","triggerScenarios":"ResourceReadyEvent firing before/without OnBeforeResourceCompleted having successfully initialized the clients, e.g. if startup event handling was skipped or failed silently.","commonSituations":"Unusual startup ordering, event subscription issues, or exceptions swallowed during client initialization followed by a ready signal.","solutions":["Update Aspire packages and retry; this indicates an internal lifecycle invariant violation","Check logs for earlier failures in the OnBeforeResourceStarted handler that left clients null","File an issue with repro details if the error reproduces on a clean setup"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if (blobServiceClient is null || queueServiceClient is null)\n    throw new InvalidOperationException(\"Emulator clients not initialized; resource start event did not complete.\");","typeGuard":null,"tryCatchPattern":"try { await onResourceReady(ct); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"not initialized\")) { logger.LogError(ex, \"Emulator client lifecycle invariant violated\"); throw; }","preventionTips":["Preserve the startup event ordering that initializes emulator clients","Treat this error as a bug report trigger, not a recoverable condition","Add integration tests covering emulator resource readiness"],"tags":["azure","storage","emulator","lifecycle","invariant"],"backgroundTag":"internal-invariant-violation","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}