microsoft/aspire · critical · DistributedApplicationException

ConnectionStringAvailableEvent was published for the

Error message

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

What it means

AddPostgres subscribes to ConnectionStringAvailableEvent for the PostgresServerResource and caches the value of GetConnectionStringAsync. If the event fires but the connection string is null, the resource model is inconsistent (the event guarantees a value), so a DistributedApplicationException is thrown. The cached string is later used for database creation and health checks.

Solutions

  1. Ensure the Postgres server resource has its username/password parameters and endpoint configured (defaults from AddPostgres).
  2. If overriding the connection string expression, guarantee a non-null result or fail earlier with a clearer error.
  3. Avoid publishing ConnectionStringAvailableEvent manually before the resource model is complete.
Defensive patterns

Strategy: validation

Validate before calling

// Ensure username/password parameters exist so the connection string resolves
if (builder.Configuration["Parameters:postgres-username"] is null ||
    builder.Configuration["Parameters:postgres-password"] is null)
{
    // defaults exist, but custom overrides must supply values
    logger.LogWarning("Postgres parameters missing; connection string may not resolve.");
}

Try / catch

try { var cs = await postgresServer.GetConnectionStringAsync(ct); } catch (DistributedApplicationException ex) { logger.LogError(ex, "Postgres connection string invariant broken"); throw; }

Prevention

When it happens

Trigger: ConnectionStringAvailableEvent published for postgresServer while GetConnectionStringAsync returns null — e.g. a missing/invalid parameter or endpoint backing the connection string expression.

Common situations: Custom Postgres resource overrides that produce a null expression result; running the AppHost without required configuration; premature manual event publication.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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

Appendix: source

Thrown at src/Aspire.Hosting.PostgreSQL/PostgresBuilderExtensions.cs:69

        IResourceBuilder<ParameterResource>? password = null,
        int? port = null)
    {
        ArgumentNullException.ThrowIfNull(builder);
        ArgumentException.ThrowIfNullOrEmpty(name);

        var passwordParameter = password?.Resource ?? ParameterResourceBuilderExtensions.CreateDefaultPasswordParameter(builder, $"{name}-password");

        var postgresServer = new PostgresServerResource(name, userName?.Resource, passwordParameter);

        string? connectionString = null;

        builder.Eventing.Subscribe<ConnectionStringAvailableEvent>(postgresServer, async (@event, ct) =>
        {
            connectionString = await postgresServer.GetConnectionStringAsync(ct).ConfigureAwait(false);

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

        builder.Eventing.Subscribe<ResourceReadyEvent>(postgresServer, async (@event, ct) =>
        {
            if (connectionString is null)
            {
                throw new DistributedApplicationException($"ResourceReadyEvent was published for the '{postgresServer.Name}' resource but the connection string was null.");
            }

            // Non-database scoped connection string
            using var npgsqlConnection = new NpgsqlConnection(connectionString + ";Database=postgres;");

            await npgsqlConnection.OpenAsync(ct).ConfigureAwait(false);

            if (npgsqlConnection.State != System.Data.ConnectionState.Open)
            {
                throw new InvalidOperationException($"Could not open connection to '{postgresServer.Name}'");

View on GitHub (pinned to 25830f84bd)