microsoft/aspire · error · DistributedApplicationException

ConnectionStringAvailableEvent published for resource

Error message

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

What it means

AzureKustoBuilderExtensions registers a handler for the ConnectionStringAvailableEvent on the Kusto database resource. The handler resolves the resource's ConnectionStringExpression; a null result is impossible under normal eventing invariants (the event means a connection string is available), so a null is surfaced as a DistributedApplicationException naming the resource. It also assigns the parsed KustoConnectionStringBuilder for the subsequent health check.

Solutions

  1. Ensure the parent AddAzureKustoCluster resource is configured so it supplies a connection string to its databases.
  2. If overriding ConnectionString via WithConnectionString/callback, guarantee it returns a non-null value (e.g. a valid Kusto connection string).
  3. Check for model mutations (removing the cluster or clearing its connection string) before the database resource runs.
  4. In tests, do not publish ConnectionStringAvailableEvent manually without setting the connection string.

Example fix

// before
.WithConnectionString(_ => null) // handler gets null

// after
.WithConnectionString("Data Source=https://mycluster.kusto.windows.net;...")
Defensive patterns

Strategy: type-guard

Validate before calling

var cs = await db.ConnectionStringExpression.GetValueAsync(ct);
if (cs is null)
    throw new InvalidOperationException("Kusto connection string was not populated; check the parent cluster resource configuration.");

Type guard

string? GuardConnectionString(object? value) => value as string is { Length: > 0 } s ? s : null;

Try / catch

try { await StartAsync(); }
catch (DistributedApplicationException ex) when (ex.Message.Contains("connection string was null"))
{
    // fix the cluster resource / connection-string override before retry
}

Prevention

When it happens

Trigger: The OnConnectionStringAvailable callback fires for the Kusto database and db.ConnectionStringExpression.GetValueAsync(ct) returns null - e.g. the connection string callback was never populated because the parent Kusto cluster resource is misconfigured, a custom ConnectionString override returns null, or the resource model was mutated before the event fired.

Common situations: Manually overriding the connection string with a callback that returns null; using the database resource without a properly configured cluster resource; tests or custom host code publishing ConnectionStringAvailableEvent without actually setting the connection string.

Related errors


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

Appendix: source

Thrown at src/Aspire.Hosting.Azure.Kusto/AzureKustoBuilderExtensions.cs:128

    public static IResourceBuilder<AzureKustoReadWriteDatabaseResource> AddReadWriteDatabase(this IResourceBuilder<AzureKustoClusterResource> builder, [ResourceName] string name, string? databaseName = null)
    {
        ArgumentNullException.ThrowIfNull(builder);
        ArgumentException.ThrowIfNullOrWhiteSpace(name);

        // Use the resource name as the database name if it's not provided
        databaseName ??= name;

        var kustoDatabase = new AzureKustoReadWriteDatabaseResource(name, databaseName, builder.Resource);
        builder.Resource.Databases.Add(kustoDatabase);
        var resourceBuilder = builder.ApplicationBuilder.AddResource(kustoDatabase)
            .WithIconName("Database");

        // Register a health check that will be used to verify database is available
        KustoConnectionStringBuilder? kcsb = null;
        resourceBuilder.OnConnectionStringAvailable(async (db, evt, ct) =>
        {
            var connectionString = await db.ConnectionStringExpression.GetValueAsync(ct).ConfigureAwait(false) ??
            throw new DistributedApplicationException($"ConnectionStringAvailableEvent published for resource '{db.Name}', but the connection string was null.");

            kcsb = GetConnectionStringBuilder(builder.Resource, connectionString);
        });

        var healthCheckKey = $"{kustoDatabase.Name}_check";
        resourceBuilder.ApplicationBuilder
            .Services
            .AddHealthChecks()
            .AddAzureKustoHealthCheck(healthCheckKey, isCluster: false, _ => kcsb!);

        resourceBuilder
            .WithHealthCheck(healthCheckKey);

        return resourceBuilder;
    }

    /// <summary>
    /// Configures the Kusto resource to run as an emulator using the Kustainer container.

View on GitHub (pinned to 25830f84bd)