microsoft/aspire · critical · DistributedApplicationException

ConnectionStringAvailableEvent was published for the

Error message

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

What it means

When AddDatabase builds a Postgres database resource, it subscribes to ConnectionStringAvailableEvent to capture the resolved connection string. If the event fires but postgresDatabase.ConnectionStringExpression evaluates to null, the library considers the resource model broken and throws DistributedApplicationException rather than proceeding with a null connection string.

Solutions

  1. Verify the parent Postgres server resource (created via AddPostgres) is correctly configured and its connection string is generated.
  2. Check for customizations (WithConnectionRedirection, annotation removal, model transformations) that stripped the database resource's ConnectionStringExpression.
  3. Ensure you did not remove or replace the parent resource after calling AddDatabase.
  4. Reproduce with dashboard logs to see why the parent's connection string callback returned null.

Example fix

// before
var db = postgres.AddDatabase("db"); // parent postgres resource customized/misconfigured
// after
var postgres = builder.AddPostgres("postgres");
var db = postgres.AddDatabase("mydb"); // fresh, correctly configured parent
Defensive patterns

Strategy: validation

Validate before calling

if (postgres is null || postgres.Resource is not PostgresServerResource server || server is null)
    throw new InvalidOperationException("Parent Postgres server resource is not configured before AddDatabase.");

Type guard

var hasConnString = postgres.Resource is IResourceWithConnectionString { ConnectionStringExpression: not null };

Prevention

When it happens

Trigger: Calling AddDatabase on a PostgresDatabaseResource whose ConnectionStringExpression cannot resolve when the ConnectionStringAvailableEvent is published — e.g. the parent postgres server resource never produced a connection string callback.

Common situations: Modeling a Postgres database against a parent server that was misconfigured or replaced; custom resource overrides that clear the ConnectionStringResource/ConnectionStringExpression; running the AppHost in an environment where the parent's host/port endpoints never materialize.

Related errors


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

Appendix: source

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

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

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

        var postgresDatabase = new PostgresDatabaseResource(name, databaseName, builder.Resource);

        builder.Resource.AddDatabase(postgresDatabase.Name, postgresDatabase.DatabaseName);

        string? connectionString = null;

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

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

        var healthCheckKey = $"{name}_check";
        builder.ApplicationBuilder.Services.AddHealthChecks().AddNpgSql(sp => connectionString ?? throw new InvalidOperationException("Connection string is unavailable"), name: healthCheckKey);

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

    /// <summary>
    /// Adds a pgAdmin 4 administration and development platform for PostgreSQL to the application model.
    /// </summary>
    /// <remarks>
    /// This version of the package defaults to the <inheritdoc cref="PostgresContainerImageTags.PgAdminTag"/> tag of the <inheritdoc cref="PostgresContainerImageTags.PgAdminImage"/> container image.
    /// </remarks>

View on GitHub (pinned to 25830f84bd)