{"record":{"id":"60cdf0e6a2e1db12","repo":"microsoft/aspire","slug":"connectionstringavailableevent-was-published-for-the","errorCode":null,"errorMessage":"ConnectionStringAvailableEvent was published for the '{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.MongoDB/MongoDBBuilderExtensions.cs","lineNumber":101,"sourceCode":"                // NOTE: Without a database as the target of the healthcheck, the healthcheck runs a `listDatabases` command against the Mongo server. This is problematic in cases where the Mongo server is a replica set secondary node, because during the phase in which the replica set is being initialized, the secondary node will return an error when `listDatabases` is called. To avoid this, we specify a database to use for the healthcheck. The healthcheck will then run a `ping` command against the specified database instead of `listDatabases`, which works even on a secondary node during replica set initialization.\n                databaseNameFactory: _ => mongoServerResource.Databases.Values.FirstOrDefault(defaultValue: MongoDBServerResource.DefaultAuthenticationDatabase)\n            );\n\n        var mongoBuilder = builder\n            .AddResource(mongoServerResource)\n            .WithEndpoint(port: port, targetPort: DefaultContainerPort, name: MongoDBServerResource.PrimaryEndpointName)\n            .WithImage(MongoDBContainerImageTags.Image, MongoDBContainerImageTags.Tag)\n            .WithImageRegistry(MongoDBContainerImageTags.Registry)\n            .WithIconName(\"DatabaseMultiple\")\n            .WithEnvironment(context =>\n            {\n                context.EnvironmentVariables[UserEnvVarName] = mongoServerResource.UserNameReference;\n                context.EnvironmentVariables[PasswordEnvVarName] = mongoServerResource.PasswordParameter!;\n            })\n            .OnConnectionStringAvailable(async (resource, @event, ct) =>\n            {\n                connectionString = await resource.ConnectionStringExpression.GetValueAsync(ct).ConfigureAwait(false)\n                    ?? throw new DistributedApplicationException($\"ConnectionStringAvailableEvent was published for the '{resource.Name}' resource but the connection string was null.\");\n            })\n            .WithHealthCheck(healthCheckKey)\n            .WithCertificateTrustConfiguration(context =>\n            {\n                // NOTE: `mongod` refuses to start when it is handed TLS file arguments without TLS actually being turned on, so these are only added once the endpoint has been marked as TLS-enabled.\n                if (mongoServerResource.TlsEnabled)\n                {\n                    context.Arguments.Add(\"--tlsCAFile\");\n                    context.Arguments.Add(context.CertificateBundlePath);\n                }\n\n                return Task.CompletedTask;\n            })\n            .WithHttpsCertificateConfiguration(context =>\n            {\n                if (mongoServerResource.TlsEnabled)\n                {\n                    context.Arguments.Add(\"--tlsCertificateKeyFile\");","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.MongoDB/MongoDBBuilderExtensions.cs#L83-L119","documentation":"When the ConnectionStringAvailableEvent fires for the Mongo server resource, AddMongoDB re-evaluates the resource's ConnectionStringExpression and throws a DistributedApplicationException if the result is null. The event contract promises a connection string, so a null value means the resource's connection string expression failed to produce a value.","triggerScenarios":"The MongoServerResource's ConnectionStringExpression evaluates to null when the ConnectionStringAvailableEvent handler calls GetValueAsync — e.g. the expression references a missing parameter or endpoint.","commonSituations":"Removing the default endpoint from the Mongo server resource, deleting the password/user parameter it references, or building the resource manually without setting ConnectionStringExpression.","solutions":["Verify the Mongo server resource keeps its default endpoint and ConnectionStringExpression (don't remove annotations added by AddMongoDB).","If customizing credentials, ensure the referenced parameters (username/password) are defined via AddParameter.","Check the resource name doesn't collide with another resource that replaced its connection string configuration."],"exampleFix":"// before\nvar mongo = builder.AddMongoDB(\"mongo\");\nmongo.Resource.ConnectionStrings.Clear(); // breaks expression resolution\n// after\nvar mongo = builder.AddMongoDB(\"mongo\");","handlingStrategy":"try-catch","validationCode":"var cs = await mongoResource.ConnectionStringExpression.GetValueAsync(default);\nif (cs is null) throw new InvalidOperationException(\"Mongo connection string expression resolved to null\");","typeGuard":null,"tryCatchPattern":"try { /* start app host */ } catch (DistributedApplicationException ex) when (ex.Message.Contains(\"ConnectionStringAvailableEvent\")) { /* fix connection string expression on the named resource */ throw; }","preventionTips":["Never clear ConnectionStringExpression or related annotations on the server resource.","Define username/password parameters before referencing them.","Check app host logs for connection string resolution warnings."],"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-21T04:17:39.646Z"}