{"record":{"id":"ca865e69ea9a8034","repo":"microsoft/aspire","slug":"connectionstringavailableevent-was-published-for-the-name-ca865e","errorCode":null,"errorMessage":"ConnectionStringAvailableEvent was published for the '{name}' resource but the connection string was null.","messagePattern":"ConnectionStringAvailableEvent was published for the '(.+?)' resource but the connection string was null\\.","errorType":"exception","errorClass":"DistributedApplicationException","httpStatus":null,"severity":"critical","filePath":"src/Aspire.Hosting.PostgreSQL/PostgresBuilderExtensions.cs","lineNumber":168,"sourceCode":"        ArgumentNullException.ThrowIfNull(builder);\n        ArgumentException.ThrowIfNullOrEmpty(name);\n\n        // Use the resource name as the database name if it's not provided\n        databaseName ??= name;\n\n        var postgresDatabase = new PostgresDatabaseResource(name, databaseName, builder.Resource);\n\n        builder.Resource.AddDatabase(postgresDatabase.Name, postgresDatabase.DatabaseName);\n\n        string? connectionString = null;\n\n        builder.ApplicationBuilder.Eventing.Subscribe<ConnectionStringAvailableEvent>(postgresDatabase, async (@event, ct) =>\n        {\n            connectionString = await postgresDatabase.ConnectionStringExpression.GetValueAsync(ct).ConfigureAwait(false);\n\n            if (connectionString == null)\n            {\n                throw new DistributedApplicationException($\"ConnectionStringAvailableEvent was published for the '{name}' resource but the connection string was null.\");\n            }\n        });\n\n        var healthCheckKey = $\"{name}_check\";\n        builder.ApplicationBuilder.Services.AddHealthChecks().AddNpgSql(sp => connectionString ?? throw new InvalidOperationException(\"Connection string is unavailable\"), name: healthCheckKey);\n\n        return builder.ApplicationBuilder\n            .AddResource(postgresDatabase)\n            .WithIconName(\"Database\")\n            .WithHealthCheck(healthCheckKey);\n    }\n\n    /// <summary>\n    /// Adds a pgAdmin 4 administration and development platform for PostgreSQL to the application model.\n    /// </summary>\n    /// <remarks>\n    /// This version of the package defaults to the <inheritdoc cref=\"PostgresContainerImageTags.PgAdminTag\"/> tag of the <inheritdoc cref=\"PostgresContainerImageTags.PgAdminImage\"/> container image.\n    /// </remarks>","sourceCodeStart":150,"sourceCodeEnd":186,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.PostgreSQL/PostgresBuilderExtensions.cs#L150-L186","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the parent Postgres server resource (created via AddPostgres) is correctly configured and its connection string is generated.","Check for customizations (WithConnectionRedirection, annotation removal, model transformations) that stripped the database resource's ConnectionStringExpression.","Ensure you did not remove or replace the parent resource after calling AddDatabase.","Reproduce with dashboard logs to see why the parent's connection string callback returned null."],"exampleFix":"// before\nvar db = postgres.AddDatabase(\"db\"); // parent postgres resource customized/misconfigured\n// after\nvar postgres = builder.AddPostgres(\"postgres\");\nvar db = postgres.AddDatabase(\"mydb\"); // fresh, correctly configured parent","handlingStrategy":"validation","validationCode":"if (postgres is null || postgres.Resource is not PostgresServerResource server || server is null)\n    throw new InvalidOperationException(\"Parent Postgres server resource is not configured before AddDatabase.\");","typeGuard":"var hasConnString = postgres.Resource is IResourceWithConnectionString { ConnectionStringExpression: not null };","tryCatchPattern":null,"preventionTips":["Always derive database resources from a properly configured AddPostgres server resource.","Avoid removing/replacing parent resources after calling AddDatabase.","Review custom model transformations that touch ConnectionStringExpression."],"tags":["csharp","aspire","postgresql","connection-string"],"backgroundTag":"null-argument","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T09:17:21.228Z"}