{"record":{"id":"4d326710a11442be","repo":"microsoft/aspire","slug":"connectionstringavailableevent-was-published-for-the-name","errorCode":null,"errorMessage":"ConnectionStringAvailableEvent was published for the '{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":136,"sourceCode":"        ArgumentNullException.ThrowIfNull(builder);\n        ArgumentException.ThrowIfNullOrEmpty(name);\n\n        // Use the resource name as the database name if it's not provided\n        databaseName ??= name;\n\n        var mySqlDatabase = new MySqlDatabaseResource(name, databaseName, builder.Resource);\n\n        builder.Resource.AddDatabase(mySqlDatabase);\n\n        string? connectionString = null;\n\n        builder.ApplicationBuilder.Eventing.Subscribe<ConnectionStringAvailableEvent>(mySqlDatabase, async (@event, ct) =>\n        {\n            connectionString = await mySqlDatabase.ConnectionStringExpression.GetValueAsync(ct).ConfigureAwait(false);\n\n            if (connectionString is null)\n            {\n                throw new DistributedApplicationException($\"ConnectionStringAvailableEvent was published for the '{name}' resource but the connection string was null.\");\n            }\n        });\n\n        var healthCheckKey = $\"{name}_check\";\n        builder.ApplicationBuilder.Services.AddHealthChecks().AddMySql(sp => connectionString ?? throw new InvalidOperationException(\"Connection string is unavailable\"), name: healthCheckKey);\n\n        return builder.ApplicationBuilder\n            .AddResource(mySqlDatabase)\n            .WithIconName(\"Database\")\n            .WithHealthCheck(healthCheckKey);\n    }\n\n    private static async Task CreateDatabaseAsync(MySqlConnection sqlConnection, MySqlDatabaseResource sqlDatabase, IServiceProvider serviceProvider, CancellationToken ct)\n    {\n        var logger = serviceProvider.GetRequiredService<ResourceLoggerService>().GetLogger(sqlDatabase.Parent);\n\n        logger.LogDebug(\"Creating database '{DatabaseName}'\", sqlDatabase.DatabaseName);\n","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.MySql/MySqlBuilderExtensions.cs#L118-L154","documentation":"AddDatabase subscribes to ConnectionStringAvailableEvent for the MySqlDatabaseResource and caches its connection string. The database resource's connection string derives from its parent server plus the database name; if evaluation returns null, the expected invariant is violated and a DistributedApplicationException is thrown.","triggerScenarios":"ConnectionStringAvailableEvent published for a MySqlDatabaseResource whose ConnectionStringExpression resolves to null — usually a misconfigured parent server resource or a tampered connection string expression.","commonSituations":"Custom database resource creation bypassing reference.AddDatabase; modifications to the parent server's connection string expression after creation; unusual manual resource graph construction in tests.","solutions":["Create database resources via the standard serverResourceBuilder.AddDatabase(name) API so the expression is wired correctly.","Do not null out or replace the parent server's ConnectionStringExpression.","Ensure the parent MySQL server resource is configured before referencing it."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"var cs = await mySqlDatabase.ConnectionStringExpression.GetValueAsync(ct);\nif (cs is null) throw new InvalidOperationException(\"MySQL database connection string unresolved.\");","typeGuard":"bool HasDatabaseExpression(IResource db) => db.ConnectionStringExpression is not null;","tryCatchPattern":"try { /* use database resource */ } catch (DistributedApplicationException ex) { logger.LogError(ex, \"MySQL database connection string null\"); throw; }","preventionTips":["Create databases only via serverResourceBuilder.AddDatabase.","Keep the parent server resource's connection string expression untouched.","Avoid hand-built resource graphs in production code."],"tags":["mysql","database","connection-string","eventing"],"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"}