{"record":{"id":"e41c08f68cf80a13","repo":"microsoft/aspire","slug":"connection-string-is-unavailable-mysqlbuilderextensions","errorCode":null,"errorMessage":"Connection string is unavailable","messagePattern":"Connection string is unavailable","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.MySql/MySqlBuilderExtensions.cs","lineNumber":79,"sourceCode":"                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                      {\n                          context.EnvironmentVariables[PasswordEnvVarName] = resource.PasswordParameter;\n                      })\n                      .WithHealthCheck(healthCheckKey);\n    }\n\n    /// <summary>\n    /// Adds a MySQL database to the application model.\n    /// </summary>\n    /// <param name=\"builder\">The MySQL server resource builder.</param>\n    /// <param name=\"name\">The name of the resource. This name will be used as the connection string name when referenced in a dependency.</param>","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.MySql/MySqlBuilderExtensions.cs#L61-L97","documentation":"AddMySql registers a health check whose factory reads the captured connectionString closure; if the health check runs before the ConnectionStringAvailableEvent handler has populated it, the factory throws this InvalidOperationException so the health check reports failure rather than silently using a null connection string.","triggerScenarios":"Health check executes before connectionString is assigned — the health check for '{name}_check' runs while the resource's connection string is still unresolved.","commonSituations":"Startup ordering where health checks execute before the connection-string event; extremely fast app startup against a slow event pipeline; custom lifecycles that delay ConnectionStringAvailableEvent.","solutions":["Let the built-in health check gating work: the check failing is transient; ensure something waits on the resource's health before starting dependents.","If writing custom checks, guard against null and return unhealthy instead of throwing.","Investigate why the connection string event hasn't fired (resource configuration) rather than working around the check."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":"if (connectionString is null) return HealthCheckResult.Unhealthy(\"Connection string not yet available\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Gate dependents on health checks instead of starting them immediately.","Don't force health check execution before the resource events fire.","Investigate root cause if the connection string event never arrives."],"tags":["mysql","health-check","connection-string","null"],"backgroundTag":"missing-config-value","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"}