{"record":{"id":"6133e828b68422bd","repo":"microsoft/aspire","slug":"resourcereadyevent-was-published-for-the-resource-name","errorCode":null,"errorMessage":"ResourceReadyEvent was published for the '{resource.Name}' resource but the connection string was null.","messagePattern":"ResourceReadyEvent was published for the '(.+?)' resource but the connection string was null\\.","errorType":"exception","errorClass":"DistributedApplicationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.MySql/MySqlBuilderExtensions.cs","lineNumber":61,"sourceCode":"        var resource = new MySqlServerResource(name, passwordParameter);\n\n        string? connectionString = null;\n\n        builder.Eventing.Subscribe<ConnectionStringAvailableEvent>(resource, async (@event, ct) =>\n        {\n            connectionString = await resource.ConnectionStringExpression.GetValueAsync(ct).ConfigureAwait(false);\n\n            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);","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.MySql/MySqlBuilderExtensions.cs#L43-L79","documentation":"In AddMySql, the ResourceReadyEvent handler uses the connection string captured earlier to open a MySqlConnection and create requested databases. If the captured connectionString is still null when the resource reports ready, the earlier ConnectionStringAvailableEvent never delivered a value, so this DistributedApplicationException is thrown.","triggerScenarios":"ResourceReadyEvent fires for the MySQL resource while the closure variable connectionString is null — i.e. ConnectionStringAvailableEvent never ran or ran with a null result before readiness.","commonSituations":"Resource becomes ready without the connection-string-available event having fired (custom lifecycle, eventing misconfiguration); ordering anomalies in custom resource models that skip standard event publication.","solutions":["Verify the resource uses the standard Aspire MySQL resource lifecycle so both events are published in order.","Do not suppress or replace the ConnectionStringAvailableEvent subscription when customizing the builder.","Inspect custom eventing code that may publish ResourceReadyEvent without first resolving the connection string."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { /* use resource */ } catch (DistributedApplicationException ex) { logger.LogError(ex, \"MySQL connection string was null on ResourceReadyEvent\"); throw; }","preventionTips":["Use the standard Aspire MySQL lifecycle; don't publish ResourceReadyEvent manually.","Preserve the AddMySql event subscriptions when customizing the builder.","Check event ordering in custom lifecycle code."],"tags":["mysql","connection-string","eventing","lifecycle"],"backgroundTag":"internal-invariant-violation","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"}