{"record":{"id":"c13c80ae56512ae1","repo":"microsoft/aspire","slug":"connection-string-is-unavailable-c13c80","errorCode":null,"errorMessage":"Connection string is unavailable","messagePattern":"Connection string is unavailable","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.MongoDB/ReplicaSet/MongoDBReplicaSetBuilderExtensions.cs","lineNumber":99,"sourceCode":"                new GenerateParameterDefault\n                {\n                    MinLength = 512, // NOTE: MongoDB requires the key file content to be between 6 and 1024 characters — see https://www.mongodb.com/docs/manual/tutorial/deploy-replica-set-with-keyfile-access-control/#create-a-keyfile\n                    Special = false,\n                }\n            ),\n            sharedUserName: userName?.Resource,\n            sharedPassword: password?.Resource\n                ?? ParameterResourceBuilderExtensions.CreateDefaultPasswordParameter(builder, $\"{name}-password\", special: false)\n        );\n\n        var connectionString = null as string;\n        var healthCheckKey = $\"{name}_check\";\n\n        // NOTE: `clientFactory` is invoked every time the healthcheck is performed. We cache the client so it is reused.\n        var client = null as IMongoClient;\n        builder.Services.AddHealthChecks()\n            .AddMongoDb(\n                sp => client ??= new MongoClient(connectionString ?? throw new InvalidOperationException(\"Connection string is unavailable\")),\n                name: healthCheckKey);\n\n        return builder.AddResource(rsResource)\n            .WithHealthCheck(healthCheckKey)\n            .WithInitialState(new()\n            {\n                ResourceType = \"MongoDB Replica Set\",\n                CreationTimeStamp = DateTime.UtcNow,\n                State = KnownResourceStates.Waiting,\n                Properties = [],\n            })\n            .OnInitializeResource(async (resource, evt, ct) =>\n            {\n                // NOTE: `evt.Logger` is backed by `ResourceLoggerService` for this resource, so what is logged here shows up\n                // in this resource's console in the dashboard. A category logger would only reach the app host log, which is\n                // the wrong place for diagnostics about why this resource failed to start.\n                var logger = evt.Logger;\n","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.MongoDB/ReplicaSet/MongoDBReplicaSetBuilderExtensions.cs#L81-L117","documentation":"AddMongoDBReplicaSet registers a health check that creates a MongoClient from the replica set's connection string. When the connection string expression resolves to null/empty at health-check time, it throws InvalidOperationException('Connection string is unavailable').","triggerScenarios":"The replica set member's connection string callback returns null, typically because the underlying MongoDB server resource has no connection string available (e.g. missing parameters, no endpoints resolved, or the resource is not properly initialized) when the health check first runs.","commonSituations":"Running an app host where a MongoDB member's connection string depends on an unfulfilled parameter or an endpoint that never got allocated; race conditions where the health check runs before connection string generation is possible.","solutions":["Ensure the MongoDB server resources backing the replica set are fully configured (reference parameters resolved, endpoints defined)","Verify the connection string is supplied via AddConnectionString/parameter before the replica set member is created","Check that AddMongoDBReplicaSet is called on a resource that exposes a valid ConnectionStringExpression"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"var cs = await memberResource.ConnectionStringExpression.GetValueAsync(ct);\nif (string.IsNullOrEmpty(cs)) throw new InvalidOperationException(\"Member connection string must be configured before adding a replica set\");","typeGuard":null,"tryCatchPattern":"try { builder.AddMongoDBReplicaSet(...); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Connection string is unavailable\")) { /* verify parameters/connection string source */ }","preventionTips":["Resolve all reference parameters before app start","Confirm each member has endpoints and a connection string expression","Run the app host with the dashboard open to catch missing connection strings early"],"tags":["mongodb","replica-set","connection-string","health-check"],"backgroundTag":"missing-credentials","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"}