{"record":{"id":"aceaa49e4b37f1f7","repo":"microsoft/aspire","slug":"connectionstringavailableevent-was-published-for-the-aceaa4","errorCode":null,"errorMessage":"ConnectionStringAvailableEvent was published for the '{resource.Name}' resource but the connection string was null.","messagePattern":"ConnectionStringAvailableEvent 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":53,"sourceCode":"    [AspireExport]\n    public static IResourceBuilder<MySqlServerResource> AddMySql(this IDistributedApplicationBuilder builder, [ResourceName] string name, IResourceBuilder<ParameterResource>? password = null, int? port = null)\n    {\n        ArgumentNullException.ThrowIfNull(builder);\n        ArgumentException.ThrowIfNullOrEmpty(name);\n\n        var passwordParameter = password?.Resource ?? ParameterResourceBuilderExtensions.CreateDefaultPasswordParameter(builder, $\"{name}-password\");\n\n        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","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.MySql/MySqlBuilderExtensions.cs#L35-L71","documentation":"AddMySql subscribes to ConnectionStringAvailableEvent for the MySQL server resource and captures its connection string. The event should only fire once the value is resolvable; if GetValueAsync still returns null, the resource graph is in an unexpected state, so a DistributedApplicationException is thrown to surface the invariant violation immediately.","triggerScenarios":"ConnectionStringAvailableEvent is published for a MySqlServerResource whose ConnectionStringExpression evaluates to null — typically a misconfigured or removed endpoint/parameter backing the connection string expression.","commonSituations":"Custom resource subclasses or manual resource model edits that bypass the normal connection-string assignment; bugs introduced when replacing the resource's ConnectionStringExpression with one returning null.","solutions":["Ensure the MySQL server resource has a valid connection string expression (default AddMySql setup provides one; don't clear it).","If customizing the resource, set ConnectionStringResource/ConnectionStringExpression before the event can fire.","Check for code that resets or replaces the resource's connection string after creation."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"var cs = await resource.ConnectionStringExpression.GetValueAsync(ct);\nif (cs is null) throw new InvalidOperationException(\"MySQL server connection string unresolved.\");","typeGuard":"bool HasConnectionString(IResource r) => r.ConnectionStringExpression is not null;","tryCatchPattern":"try { /* start resource */ } catch (DistributedApplicationException ex) { logger.LogError(ex, \"MySQL connection string was null on ConnectionStringAvailableEvent\"); }","preventionTips":["Keep the default AddMySql resource configuration intact.","Don't clear or replace ConnectionStringExpression after resource creation.","Review custom resource-model code for null connection strings."],"tags":["mysql","connection-string","eventing","invariant"],"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"}