microsoft/aspire · error · DistributedApplicationException

ConnectionStringAvailableEvent was published for the

Error message

ConnectionStringAvailableEvent was published for the '{resource.Name}' resource but the connection string was null.

What it means

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.

Solutions

  1. Verify the Mongo server resource keeps its default endpoint and ConnectionStringExpression (don't remove annotations added by AddMongoDB).
  2. If customizing credentials, ensure the referenced parameters (username/password) are defined via AddParameter.
  3. Check the resource name doesn't collide with another resource that replaced its connection string configuration.

Example fix

// before
var mongo = builder.AddMongoDB("mongo");
mongo.Resource.ConnectionStrings.Clear(); // breaks expression resolution
// after
var mongo = builder.AddMongoDB("mongo");
Defensive patterns

Strategy: try-catch

Validate before calling

var cs = await mongoResource.ConnectionStringExpression.GetValueAsync(default);
if (cs is null) throw new InvalidOperationException("Mongo connection string expression resolved to null");

Try / catch

try { /* start app host */ } catch (DistributedApplicationException ex) when (ex.Message.Contains("ConnectionStringAvailableEvent")) { /* fix connection string expression on the named resource */ throw; }

Prevention

When it happens

Trigger: The MongoServerResource's ConnectionStringExpression evaluates to null when the ConnectionStringAvailableEvent handler calls GetValueAsync — e.g. the expression references a missing parameter or endpoint.

Common situations: Removing the default endpoint from the Mongo server resource, deleting the password/user parameter it references, or building the resource manually without setting ConnectionStringExpression.

Understand the failure class

Background: "missing required config value" errors: why libraries refuse to start when a configuration key is empty, unset, or blank — this error's family across 48 libraries.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/60cdf0e6a2e1db12. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting.MongoDB/MongoDBBuilderExtensions.cs:101

                // 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.
                databaseNameFactory: _ => mongoServerResource.Databases.Values.FirstOrDefault(defaultValue: MongoDBServerResource.DefaultAuthenticationDatabase)
            );

        var mongoBuilder = builder
            .AddResource(mongoServerResource)
            .WithEndpoint(port: port, targetPort: DefaultContainerPort, name: MongoDBServerResource.PrimaryEndpointName)
            .WithImage(MongoDBContainerImageTags.Image, MongoDBContainerImageTags.Tag)
            .WithImageRegistry(MongoDBContainerImageTags.Registry)
            .WithIconName("DatabaseMultiple")
            .WithEnvironment(context =>
            {
                context.EnvironmentVariables[UserEnvVarName] = mongoServerResource.UserNameReference;
                context.EnvironmentVariables[PasswordEnvVarName] = mongoServerResource.PasswordParameter!;
            })
            .OnConnectionStringAvailable(async (resource, @event, ct) =>
            {
                connectionString = await resource.ConnectionStringExpression.GetValueAsync(ct).ConfigureAwait(false)
                    ?? throw new DistributedApplicationException($"ConnectionStringAvailableEvent was published for the '{resource.Name}' resource but the connection string was null.");
            })
            .WithHealthCheck(healthCheckKey)
            .WithCertificateTrustConfiguration(context =>
            {
                // 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.
                if (mongoServerResource.TlsEnabled)
                {
                    context.Arguments.Add("--tlsCAFile");
                    context.Arguments.Add(context.CertificateBundlePath);
                }

                return Task.CompletedTask;
            })
            .WithHttpsCertificateConfiguration(context =>
            {
                if (mongoServerResource.TlsEnabled)
                {
                    context.Arguments.Add("--tlsCertificateKeyFile");

View on GitHub (pinned to 25830f84bd)