microsoft/aspire · error · DistributedApplicationException
ConnectionStringAvailableEvent was published for the
Error message
ConnectionStringAvailableEvent was published for the '{name}' resource but the connection string was null. What it means
AddDatabase subscribes to ConnectionStringAvailableEvent for the MySqlDatabaseResource and caches its connection string. The database resource's connection string derives from its parent server plus the database name; if evaluation returns null, the expected invariant is violated and a DistributedApplicationException is thrown.
Solutions
- Create database resources via the standard serverResourceBuilder.AddDatabase(name) API so the expression is wired correctly.
- Do not null out or replace the parent server's ConnectionStringExpression.
- Ensure the parent MySQL server resource is configured before referencing it.
Defensive patterns
Strategy: validation
Validate before calling
var cs = await mySqlDatabase.ConnectionStringExpression.GetValueAsync(ct);
if (cs is null) throw new InvalidOperationException("MySQL database connection string unresolved."); Type guard
bool HasDatabaseExpression(IResource db) => db.ConnectionStringExpression is not null;
Try / catch
try { /* use database resource */ } catch (DistributedApplicationException ex) { logger.LogError(ex, "MySQL database connection string null"); throw; } Prevention
- Create databases only via serverResourceBuilder.AddDatabase.
- Keep the parent server resource's connection string expression untouched.
- Avoid hand-built resource graphs in production code.
When it happens
Trigger: ConnectionStringAvailableEvent published for a MySqlDatabaseResource whose ConnectionStringExpression resolves to null — usually a misconfigured parent server resource or a tampered connection string expression.
Common situations: Custom database resource creation bypassing reference.AddDatabase; modifications to the parent server's connection string expression after creation; unusual manual resource graph construction in tests.
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
- ConnectionStringAvailableEvent was published for the
- ResourceReadyEvent was published for the
- Connection string is unavailable
- ConnectionStringAvailableEvent was published for the
- ConnectionStringAvailableEvent was published for the
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/4d326710a11442be.
Report an issue: GitHub.
Appendix: source
Thrown at src/Aspire.Hosting.MySql/MySqlBuilderExtensions.cs:136
ArgumentNullException.ThrowIfNull(builder);
ArgumentException.ThrowIfNullOrEmpty(name);
// Use the resource name as the database name if it's not provided
databaseName ??= name;
var mySqlDatabase = new MySqlDatabaseResource(name, databaseName, builder.Resource);
builder.Resource.AddDatabase(mySqlDatabase);
string? connectionString = null;
builder.ApplicationBuilder.Eventing.Subscribe<ConnectionStringAvailableEvent>(mySqlDatabase, async (@event, ct) =>
{
connectionString = await mySqlDatabase.ConnectionStringExpression.GetValueAsync(ct).ConfigureAwait(false);
if (connectionString is 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().AddMySql(sp => connectionString ?? throw new InvalidOperationException("Connection string is unavailable"), name: healthCheckKey);
return builder.ApplicationBuilder
.AddResource(mySqlDatabase)
.WithIconName("Database")
.WithHealthCheck(healthCheckKey);
}
private static async Task CreateDatabaseAsync(MySqlConnection sqlConnection, MySqlDatabaseResource sqlDatabase, IServiceProvider serviceProvider, CancellationToken ct)
{
var logger = serviceProvider.GetRequiredService<ResourceLoggerService>().GetLogger(sqlDatabase.Parent);
logger.LogDebug("Creating database '{DatabaseName}'", sqlDatabase.DatabaseName);
View on GitHub (pinned to 25830f84bd)