microsoft/aspire · error · DistributedApplicationException

ConnectionStringAvailableEvent was published for the

Error message

ConnectionStringAvailableEvent was published for the '{resource.Name}' resource but the connection string was null.

What it means

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.

Solutions

  1. Ensure the MySQL server resource has a valid connection string expression (default AddMySql setup provides one; don't clear it).
  2. If customizing the resource, set ConnectionStringResource/ConnectionStringExpression before the event can fire.
  3. Check for code that resets or replaces the resource's connection string after creation.
Defensive patterns

Strategy: validation

Validate before calling

var cs = await resource.ConnectionStringExpression.GetValueAsync(ct);
if (cs is null) throw new InvalidOperationException("MySQL server connection string unresolved.");

Type guard

bool HasConnectionString(IResource r) => r.ConnectionStringExpression is not null;

Try / catch

try { /* start resource */ } catch (DistributedApplicationException ex) { logger.LogError(ex, "MySQL connection string was null on ConnectionStringAvailableEvent"); }

Prevention

When it happens

Trigger: ConnectionStringAvailableEvent is published for a MySqlServerResource whose ConnectionStringExpression evaluates to null — typically a misconfigured or removed endpoint/parameter backing the connection string expression.

Common situations: 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.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/aceaa49e4b37f1f7. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting.MySql/MySqlBuilderExtensions.cs:53

    [AspireExport]
    public static IResourceBuilder<MySqlServerResource> AddMySql(this IDistributedApplicationBuilder builder, [ResourceName] string name, IResourceBuilder<ParameterResource>? password = null, int? port = null)
    {
        ArgumentNullException.ThrowIfNull(builder);
        ArgumentException.ThrowIfNullOrEmpty(name);

        var passwordParameter = password?.Resource ?? ParameterResourceBuilderExtensions.CreateDefaultPasswordParameter(builder, $"{name}-password");

        var resource = new MySqlServerResource(name, passwordParameter);

        string? connectionString = null;

        builder.Eventing.Subscribe<ConnectionStringAvailableEvent>(resource, async (@event, ct) =>
        {
            connectionString = await resource.ConnectionStringExpression.GetValueAsync(ct).ConfigureAwait(false);

            if (connectionString == null)
            {
                throw new DistributedApplicationException($"ConnectionStringAvailableEvent was published for the '{resource.Name}' resource but the connection string was null.");
            }
        });

        builder.Eventing.Subscribe<ResourceReadyEvent>(resource, async (@event, ct) =>
        {
            if (connectionString is null)
            {
                throw new DistributedApplicationException($"ResourceReadyEvent was published for the '{resource.Name}' resource but the connection string was null.");
            }

            using var sqlConnection = new MySqlConnection(connectionString);
            await sqlConnection.OpenAsync(ct).ConfigureAwait(false);

            if (sqlConnection.State != System.Data.ConnectionState.Open)
            {
                throw new InvalidOperationException($"Could not open connection to '{resource.Name}'");
            }

View on GitHub (pinned to 25830f84bd)