microsoft/aspire · error · DistributedApplicationException

ConnectionStringAvailableEvent was published for the

Error message

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

What it means

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.

Solutions

  1. Ensure the parent Mongo server resource was created via builder.AddMongoDB so its connection string is valid.
  2. Verify the database's ConnectionStringExpression still references the parent's connection string correctly.
  3. Check for earlier exceptions or warnings in the app host log about the server resource's connection string.

Example fix

// before
var server = new MongoDBServerResource("mongo"); // built manually, no connection string
var db = server.AddDatabase("mydb");
// after
var server = builder.AddMongoDB("mongo");
var db = server.AddDatabase("mydb");
Defensive patterns

Strategy: try-catch

Validate before calling

var cs = await mongoDBDatabase.ConnectionStringExpression.GetValueAsync(default);
if (cs is null) throw new InvalidOperationException($"Database '{mongoDBDatabase.Name}' connection string is not configured");

Try / catch

try { /* start app host */ } catch (DistributedApplicationException ex) when (ex.Message.Contains("ConnectionStringAvailableEvent")) { /* verify parent server connection string */ throw; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

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/7171c907a5a70de1. Report an issue: GitHub.

Appendix: source

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

    {
        ArgumentNullException.ThrowIfNull(builder);
        ArgumentException.ThrowIfNullOrEmpty(name);

        // Use the resource name as the database name if it's not provided
        databaseName ??= name;

        builder.Resource.AddDatabase(name, databaseName);
        var mongoDBDatabase = new MongoDBDatabaseResource(name, databaseName, builder.Resource);

        string? connectionString = null;

        builder.ApplicationBuilder.Eventing.Subscribe<ConnectionStringAvailableEvent>(mongoDBDatabase, async (@event, ct) =>
        {
            connectionString = await mongoDBDatabase.ConnectionStringExpression.GetValueAsync(ct).ConfigureAwait(false);

            if (connectionString == null)
            {
                throw new DistributedApplicationException($"ConnectionStringAvailableEvent was published for the '{mongoDBDatabase.Name}' resource but the connection string was null.");
            }
        });

        var healthCheckKey = $"{name}_check";
        // cache the database client so it is reused on subsequent calls to the health check
        IMongoDatabase? database = null;
        builder.ApplicationBuilder.Services.AddHealthChecks()
            .AddMongoDb(
                sp => database ??=
                    new MongoClient(connectionString ?? throw new InvalidOperationException("Connection string is unavailable"))
                        .GetDatabase(databaseName),
                name: healthCheckKey);

        return builder.ApplicationBuilder
            .AddResource(mongoDBDatabase)
            .WithIconName("Database")
            .WithHealthCheck(healthCheckKey);
    }

View on GitHub (pinned to 25830f84bd)