{"record":{"id":"1d52f83d501e6dfd","repo":"microsoft/aspire","slug":"nameof-queueserviceclient-is-not-initialized","errorCode":null,"errorMessage":"{nameof(QueueServiceClient)} is not initialized.","messagePattern":"(.+?) is not initialized\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.Storage/AzureStorageExtensions.cs","lineNumber":230,"sourceCode":"            .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\n        builder.ApplicationBuilder.Services.AddHealthChecks().AddAzureBlobStorage(sp =>","sourceCodeStart":212,"sourceCodeEnd":248,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.Storage/AzureStorageExtensions.cs#L212-L248","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Use the stock AddAzureStorage + RunAsEmulator + AddQueues flow so the queue client factory is registered","Verify the storage resource's connection string resolution callback supplies a non-null value before ResourceReady","Update Aspire.Hosting.Azure.Storage to the latest patch to pick up initialization fixes"],"exampleFix":"// before: custom init of only blob client\nvar blob = new BlobServiceClient(conn);\n// after: initialize both clients the same way\nvar blob = new BlobServiceClient(conn);\nvar queue = new QueueServiceClient(conn);","handlingStrategy":"validation","validationCode":"if (storageResource.Resource.IsEmulator && storageResource.Resource.BlobContainers.Any())\n{\n    // Ensure the stock RunAsEmulator pipeline registered the queue client\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use the standard AddAzureStorage().RunAsEmulator().AddBlobs()/AddQueues() chain unmodified","Keep Aspire.Hosting.Azure.Storage packages at consistent versions across the app host"],"tags":["azure-storage","emulator","azurite","invalid-operation","initialization-order"],"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-21T09:17:21.228Z"}