{"record":{"id":"d609baec4bbb5974","repo":"microsoft/aspire","slug":"connectionstringavailableevent-was-published-for-the-builder","errorCode":null,"errorMessage":"ConnectionStringAvailableEvent was published for the '{builder.Resource.Name}' resource but the connection string was null.","messagePattern":"ConnectionStringAvailableEvent was published for the '(.+?)' resource but the connection string was null\\.","errorType":"exception","errorClass":"DistributedApplicationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.CosmosDB/AzureCosmosDBExtensions.cs","lineNumber":126,"sourceCode":"        builder.WithAnnotation(new EmulatorResourceAnnotation());\n\n        var scheme = useVNext ? \"http\" : null;\n        builder.WithEndpoint(name: \"emulator\", scheme: scheme, targetPort: 8081)\n               .WithAnnotation(new ContainerImageAnnotation\n               {\n                   Registry = CosmosDBEmulatorContainerImageTags.Registry,\n                   Image = CosmosDBEmulatorContainerImageTags.Image,\n                   Tag = useVNext ? CosmosDBEmulatorContainerImageTags.Tag : CosmosDBEmulatorContainerImageTags.ClassicTag\n               });\n\n        CosmosClient? cosmosClient = null;\n        builder.OnConnectionStringAvailable(async (cosmosDb, @event, ct) =>\n        {\n            var connectionString = await cosmosDb.ConnectionStringExpression.GetValueAsync(ct).ConfigureAwait(false);\n\n            if (connectionString == null)\n            {\n                throw new DistributedApplicationException($\"ConnectionStringAvailableEvent was published for the '{builder.Resource.Name}' resource but the connection string was null.\");\n            }\n\n            cosmosClient = CreateCosmosClient(connectionString);\n        })\n        .OnResourceReady(async (cosmosDb, @event, ct) =>\n        {\n            if (cosmosClient is null)\n            {\n                throw new InvalidOperationException(\"CosmosClient is not initialized.\");\n            }\n\n            await cosmosClient.ReadAccountAsync().WaitAsync(ct).ConfigureAwait(false);\n\n            foreach (var database in cosmosDb.Databases)\n            {\n                var db = (await cosmosClient.CreateDatabaseIfNotExistsAsync(database.DatabaseName, cancellationToken: ct).ConfigureAwait(false)).Database;\n\n                foreach (var container in database.Containers)","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.CosmosDB/AzureCosmosDBExtensions.cs#L108-L144","documentation":"RunAsEmulator subscribes to ConnectionStringAvailableEvent and expects the resource's ConnectionStringExpression to yield a non-null connection string by that point. If the event fires but GetValueAsync returns null, the internal lifecycle contract is broken and a DistributedApplicationException is thrown, because the emulator's CosmosClient cannot be created.","triggerScenarios":"Running a Cosmos DB resource as an emulator when the connection string expression fails to resolve, e.g. the resource model was mutated after AddAzureCosmosDB so no connection string endpoint is set, or the expression was replaced with one that returns null in run mode.","commonSituations":"Custom transformations or publish-mode mutations that strip the connection string; replacing ConnectionStringExpression with a custom reference that has no value in run mode; modifying resource annotations that the expression depends on.","solutions":["Create the resource via AddAzureCosmosDB(...).RunAsEmulator() without custom mutations to ConnectionStringExpression.","Check custom event handlers or annotations that interfere with connection string resolution.","Verify the resource named in the error message is the one actually configured as the emulator."],"exampleFix":"// before\nvar cosmos = builder.AddAzureCosmosDB(\"cosmos\");\ncosmos.Resource.ConnectionStringExpression = customExpression; // resolves to null in run mode\ncosmos.RunAsEmulator();\n// after\nvar cosmos = builder.AddAzureCosmosDB(\"cosmos\").RunAsEmulator(); // default expression resolves the emulator connection string","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { await builder.AddAzureCosmosDB(\"cosmos\").RunAsEmulatorAsync(); } catch (DistributedApplicationException ex) when (ex.Message.Contains(\"connection string was null\")) { /* inspect resource model / expression overrides */ }","preventionTips":["Do not override ConnectionStringExpression on emulator resources.","Avoid custom model mutations after RunAsEmulator.","Use the standard AddAzureCosmosDB fluent chain end to end."],"tags":["cosmosdb","emulator","connection-string"],"backgroundTag":"missing-config-value","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"}