{"record":{"id":"0006a43aab8e6102","repo":"microsoft/aspire","slug":"could-not-open-connection-to-resource-name","errorCode":null,"errorMessage":"Could not open connection to '{resource.Name}'","messagePattern":"Could not open connection to '(.+?)'","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.MySql/MySqlBuilderExtensions.cs","lineNumber":69,"sourceCode":"            if (connectionString == null)\n            {\n                throw new DistributedApplicationException($\"ConnectionStringAvailableEvent was published for the '{resource.Name}' resource but the connection string was null.\");\n            }\n        });\n\n        builder.Eventing.Subscribe<ResourceReadyEvent>(resource, async (@event, ct) =>\n        {\n            if (connectionString is null)\n            {\n                throw new DistributedApplicationException($\"ResourceReadyEvent was published for the '{resource.Name}' resource but the connection string was null.\");\n            }\n\n            using var sqlConnection = new MySqlConnection(connectionString);\n            await sqlConnection.OpenAsync(ct).ConfigureAwait(false);\n\n            if (sqlConnection.State != System.Data.ConnectionState.Open)\n            {\n                throw new InvalidOperationException($\"Could not open connection to '{resource.Name}'\");\n            }\n\n            foreach (var sqlDatabase in resource.DatabaseResources)\n            {\n                await CreateDatabaseAsync(sqlConnection, sqlDatabase, @event.Services, ct).ConfigureAwait(false);\n            }\n        });\n\n        var healthCheckKey = $\"{name}_check\";\n        builder.Services.AddHealthChecks().AddMySql(sp => connectionString ?? throw new InvalidOperationException(\"Connection string is unavailable\"), name: healthCheckKey);\n\n        return builder.AddResource(resource)\n                      .WithEndpoint(port: port, targetPort: 3306, name: MySqlServerResource.PrimaryEndpointName) // Internal port is always 3306.\n                      .WithImage(MySqlContainerImageTags.Image, MySqlContainerImageTags.Tag)\n                      .WithImageRegistry(MySqlContainerImageTags.Registry)\n                      .WithIconName(\"DatabaseMultiple\")\n                      .WithEnvironment(context =>\n                      {","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.MySql/MySqlBuilderExtensions.cs#L51-L87","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"if (resource.HealthStatus is not HealthStatus.Healthy) throw new InvalidOperationException(\"MySQL server is not healthy yet.\");","typeGuard":null,"tryCatchPattern":"try { await sqlConnection.OpenAsync(ct); } catch (MySqlException ex) { logger.LogError(ex, \"Could not open MySQL connection\"); throw; }","preventionTips":["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)."],"tags":["mysql","database","connection","health"],"backgroundTag":"database-query-failed","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}