microsoft/aspire · critical · DistributedApplicationException

ResourceReadyEvent was published for the

Error message

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

What it means

AddPostgres also subscribes to ResourceReadyEvent for the server resource to auto-create databases declared via WithDatabase. If the event fires but the cached connection string is still null, a DistributedApplicationException is thrown because database provisioning cannot proceed without a usable connection string. It indicates the connection-string event never populated the shared state before the resource was declared ready.

Solutions

  1. Fix the underlying reason the connection string did not resolve (missing parameters/endpoint config for the Postgres resource).
  2. Update the AppHost/Aspire packages — some ordering issues between these events were fixed in later versions.
  3. Avoid manually publishing ResourceReadyEvent for the resource.
Defensive patterns

Strategy: try-catch

Try / catch

try { await /* resource-ready dependent work */; } catch (DistributedApplicationException ex) when (ex.Message.Contains("ResourceReadyEvent was published")) { logger.LogError(ex, "Connection string was not resolved before resource became ready"); throw; }

Prevention

When it happens

Trigger: ResourceReadyEvent fires for postgresServer while the ConnectionStringAvailableEvent callback has not (or failed to) set the connectionString local, e.g. when the resource reports ready without a resolved connection string.

Common situations: Startup ordering issues where the resource is marked ready before its connection string resolves; failed or skipped connection-string resolution due to configuration problems; using WithDatabase with a broken server resource.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


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

Appendix: source

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

        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}'");
            }

            foreach (var name in postgresServer.Databases.Keys)
            {
                if (builder.Resources.FirstOrDefault(n => string.Equals(n.Name, name, StringComparisons.ResourceName)) is PostgresDatabaseResource postgreDatabase)
                {
                    await CreateDatabaseAsync(npgsqlConnection, postgreDatabase, @event.Services, ct).ConfigureAwait(false);
                }

View on GitHub (pinned to 25830f84bd)