{"record":{"id":"7171c907a5a70de1","repo":"microsoft/aspire","slug":"connectionstringavailableevent-was-published-for-the-7171c9","errorCode":null,"errorMessage":"ConnectionStringAvailableEvent was published for the '{mongoDBDatabase.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.MongoDB/MongoDBBuilderExtensions.cs","lineNumber":199,"sourceCode":"    {\n        ArgumentNullException.ThrowIfNull(builder);\n        ArgumentException.ThrowIfNullOrEmpty(name);\n\n        // Use the resource name as the database name if it's not provided\n        databaseName ??= name;\n\n        builder.Resource.AddDatabase(name, databaseName);\n        var mongoDBDatabase = new MongoDBDatabaseResource(name, databaseName, builder.Resource);\n\n        string? connectionString = null;\n\n        builder.ApplicationBuilder.Eventing.Subscribe<ConnectionStringAvailableEvent>(mongoDBDatabase, async (@event, ct) =>\n        {\n            connectionString = await mongoDBDatabase.ConnectionStringExpression.GetValueAsync(ct).ConfigureAwait(false);\n\n            if (connectionString == null)\n            {\n                throw new DistributedApplicationException($\"ConnectionStringAvailableEvent was published for the '{mongoDBDatabase.Name}' resource but the connection string was null.\");\n            }\n        });\n\n        var healthCheckKey = $\"{name}_check\";\n        // cache the database client so it is reused on subsequent calls to the health check\n        IMongoDatabase? database = null;\n        builder.ApplicationBuilder.Services.AddHealthChecks()\n            .AddMongoDb(\n                sp => database ??=\n                    new MongoClient(connectionString ?? throw new InvalidOperationException(\"Connection string is unavailable\"))\n                        .GetDatabase(databaseName),\n                name: healthCheckKey);\n\n        return builder.ApplicationBuilder\n            .AddResource(mongoDBDatabase)\n            .WithIconName(\"Database\")\n            .WithHealthCheck(healthCheckKey);\n    }","sourceCodeStart":181,"sourceCodeEnd":217,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.MongoDB/MongoDBBuilderExtensions.cs#L181-L217","documentation":"AddDatabase subscribes to ConnectionStringAvailableEvent for the MongoDatabaseResource and throws DistributedApplicationException if the database resource's ConnectionStringExpression evaluates to null. The database resource builds its connection string from its parent server; a null means the composed expression resolved to nothing.","triggerScenarios":"Calling AddDatabase on a Mongo server resource whose connection string expression is null or misconfigured, so the derived database connection string resolves to null when the event fires.","commonSituations":"Referencing a database from a server resource that was created outside AddMongoDB without a connection string; typos in parent resource wiring; custom resource models overriding ConnectionStringExpression with a failing reference.","solutions":["Ensure the parent Mongo server resource was created via builder.AddMongoDB so its connection string is valid.","Verify the database's ConnectionStringExpression still references the parent's connection string correctly.","Check for earlier exceptions or warnings in the app host log about the server resource's connection string."],"exampleFix":"// before\nvar server = new MongoDBServerResource(\"mongo\"); // built manually, no connection string\nvar db = server.AddDatabase(\"mydb\");\n// after\nvar server = builder.AddMongoDB(\"mongo\");\nvar db = server.AddDatabase(\"mydb\");","handlingStrategy":"try-catch","validationCode":"var cs = await mongoDBDatabase.ConnectionStringExpression.GetValueAsync(default);\nif (cs is null) throw new InvalidOperationException($\"Database '{mongoDBDatabase.Name}' connection string is not configured\");","typeGuard":null,"tryCatchPattern":"try { /* start app host */ } catch (DistributedApplicationException ex) when (ex.Message.Contains(\"ConnectionStringAvailableEvent\")) { /* verify parent server connection string */ throw; }","preventionTips":["Create parent server resources only via builder.AddMongoDB.","Avoid overriding the database resource's ConnectionStringExpression.","Run the app host locally to catch resolution failures early."],"tags":["mongodb","connection-string","eventing"],"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"}