microsoft/aspire · error · InvalidOperationException
Could not open connection to
Error message
Could not open connection to '{resource.Name}' What it means
After opening a MySqlConnection to the MySQL server resource inside the ResourceReadyEvent handler, AddMySql asserts the connection state is Open before creating databases. If OpenAsync returns but the state is not Open, this InvalidOperationException is thrown because database creation cannot proceed over a non-open connection.
Solutions
- Verify the MySQL container is healthy and fully started (check dashboard logs) before the ready event triggers database creation.
- Confirm the container image/tag is valid and the internal port 3306 is not remapped incorrectly.
- Check host networking/firewall/anti-virus interference with loopback container ports.
- Retry or pin a known-good MySQL image version if the server intermittently drops handshakes.
Defensive patterns
Strategy: retry
Validate before calling
if (resource.HealthStatus is not HealthStatus.Healthy) throw new InvalidOperationException("MySQL server is not healthy yet."); Try / catch
try { await sqlConnection.OpenAsync(ct); } catch (MySqlException ex) { logger.LogError(ex, "Could not open MySQL connection"); throw; } Prevention
- Wait for the resource to be reported healthy before consuming it.
- Verify container logs show MySQL ready for connections.
- Pin known-good MySQL image versions; check port mapping (internal 3306).
When it happens
Trigger: MySqlConnection.OpenAsync completes without reaching ConnectionState.Open for the server resource's endpoint — e.g. connection dropped during open, pooling quirks, or the server accepting TCP but failing the MySQL handshake.
Common situations: MySQL container not fully initialized (still restarting) when readiness fires; network/proxy issues between AppHost and container; wrong port mapping causing connections to something that is not MySQL.
Understand the failure class
Background: Database query failed: Internal Server Error 500s wrapping SQL, Prisma, and connection failures — what to check first — this error's family across 16 libraries.
Related errors
- ConnectionStringAvailableEvent was published for the
- -32603
- A Database could not be configured. Ensure valid connection…
- A DbContextOptions< > was not found. Please ensure…
- Already connected to
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/0006a43aab8e6102.
Report an issue: GitHub.
Appendix: source
Thrown at src/Aspire.Hosting.MySql/MySqlBuilderExtensions.cs:69
if (connectionString == null)
{
throw new DistributedApplicationException($"ConnectionStringAvailableEvent was published for the '{resource.Name}' resource but the connection string was null.");
}
});
builder.Eventing.Subscribe<ResourceReadyEvent>(resource, async (@event, ct) =>
{
if (connectionString is null)
{
throw new DistributedApplicationException($"ResourceReadyEvent was published for the '{resource.Name}' resource but the connection string was null.");
}
using var sqlConnection = new MySqlConnection(connectionString);
await sqlConnection.OpenAsync(ct).ConfigureAwait(false);
if (sqlConnection.State != System.Data.ConnectionState.Open)
{
throw new InvalidOperationException($"Could not open connection to '{resource.Name}'");
}
foreach (var sqlDatabase in resource.DatabaseResources)
{
await CreateDatabaseAsync(sqlConnection, sqlDatabase, @event.Services, ct).ConfigureAwait(false);
}
});
var healthCheckKey = $"{name}_check";
builder.Services.AddHealthChecks().AddMySql(sp => connectionString ?? throw new InvalidOperationException("Connection string is unavailable"), name: healthCheckKey);
return builder.AddResource(resource)
.WithEndpoint(port: port, targetPort: 3306, name: MySqlServerResource.PrimaryEndpointName) // Internal port is always 3306.
.WithImage(MySqlContainerImageTags.Image, MySqlContainerImageTags.Tag)
.WithImageRegistry(MySqlContainerImageTags.Registry)
.WithIconName("DatabaseMultiple")
.WithEnvironment(context =>
{View on GitHub (pinned to 25830f84bd)