microsoft/aspire · error · DistributedApplicationException

ConnectionStringAvailableEvent was published for the

Error message

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

What it means

AddSqlServer hooks the ConnectionStringAvailableEvent to capture the resource's connection string. Aspire guarantees this event fires only once a connection string should exist, so if GetConnectionStringAsync still returns null the integration treats it as a broken invariant and throws DistributedApplicationException rather than silently continuing with a null connection string.

Solutions

  1. Inspect why GetConnectionStringAsync returned null: confirm the SQL Server resource still has its primary endpoint (WithEndpoint port/targetPort 1433) and connection-string contribution intact — do not remove or override it after AddSqlServer.
  2. If you wrap or transform the resource (e.g. re-adding it under another resource type), re-apply the connection string/callback configuration that AddSqlServer registered.
  3. Update the Aspire.Hosting.SqlServer integration package if a known regression between versions fires the event before the value is ready.
  4. Reproduce with a minimal AppHost (only AddSqlServer) to rule out custom annotations/events as the cause.

Example fix

// before (custom transformation drops the connection-string setup)
var sql = builder.AddSqlServer("sql");
builder.CreateResourceBuilder(sql.Resource); // new builder, events/annotations context disturbed

// after
var sql = builder.AddSqlServer("sql");
// keep using the same builder instance so OnConnectionStringAvailable wiring stays intact
var sameBuilder = sql;
Defensive patterns

Strategy: try-catch

Try / catch

try
{
    // start/await resource lifecycle
}
catch (DistributedApplicationException ex) when (ex.Message.Contains("ConnectionStringAvailableEvent was published"))
{
    // connection-string provider is missing or broken; inspect resource annotations/endpoint config
}

Prevention

When it happens

Trigger: The ConnectionStringAvailableEvent is published for the SqlServerServerResource but sqlServer.GetConnectionStringAsync(ct) returns null inside the OnConnectionStringAvailable callback — e.g. a customized/derived resource model overrides or clears the connection-string callback, the resource's ConnectionStringExpression/endpoint resolution yields nothing, or an extension replaced the resource's connection string provider.

Common situations: Custom resource builders or model transformations that strip the endpoint or connection-string annotation from the SQL Server resource; subclassing/reimplementing the resource with an incomplete ConnectionStringSetting; upstream Aspire eventing changes firing the event earlier than the value is computed.

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/1a47cbc479a13c03. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting.SqlServer/SqlServerBuilderExtensions.cs:70

        return builder.AddResource(sqlServer)
                      .WithEndpoint(port: port, targetPort: 1433, name: SqlServerServerResource.PrimaryEndpointName)
                      .WithImage(SqlServerContainerImageTags.Image, SqlServerContainerImageTags.Tag)
                      .WithImageRegistry(SqlServerContainerImageTags.Registry)
                      .WithIconName("DatabaseMultiple")
                      .WithEnvironment("ACCEPT_EULA", "Y")
                      .WithEnvironment(context =>
                      {
                          context.EnvironmentVariables["MSSQL_SA_PASSWORD"] = sqlServer.PasswordParameter;
                      })
                      .WithHealthCheck(healthCheckKey)
                      .OnConnectionStringAvailable(async (sqlServer, @event, ct) =>
                      {
                          connectionString = await sqlServer.GetConnectionStringAsync(ct).ConfigureAwait(false);

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

                          using var sqlConnection = new SqlConnection(connectionString);
                          await sqlConnection.OpenAsync(ct).ConfigureAwait(false);

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

                          foreach (var sqlDatabase in sqlServer.DatabaseResources)

View on GitHub (pinned to 25830f84bd)