{"record":{"id":"1abfbeb1fb77aa2f","repo":"microsoft/aspire","slug":"connectionstringavailableevent-published-for-resource-db","errorCode":null,"errorMessage":"ConnectionStringAvailableEvent published for resource '{db.Name}', but the connection string was null.","messagePattern":"ConnectionStringAvailableEvent published for resource '(.+?)', but the connection string was null\\.","errorType":"exception","errorClass":"DistributedApplicationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.Kusto/AzureKustoBuilderExtensions.cs","lineNumber":128,"sourceCode":"    public static IResourceBuilder<AzureKustoReadWriteDatabaseResource> AddReadWriteDatabase(this IResourceBuilder<AzureKustoClusterResource> builder, [ResourceName] string name, string? databaseName = null)\n    {\n        ArgumentNullException.ThrowIfNull(builder);\n        ArgumentException.ThrowIfNullOrWhiteSpace(name);\n\n        // Use the resource name as the database name if it's not provided\n        databaseName ??= name;\n\n        var kustoDatabase = new AzureKustoReadWriteDatabaseResource(name, databaseName, builder.Resource);\n        builder.Resource.Databases.Add(kustoDatabase);\n        var resourceBuilder = builder.ApplicationBuilder.AddResource(kustoDatabase)\n            .WithIconName(\"Database\");\n\n        // Register a health check that will be used to verify database is available\n        KustoConnectionStringBuilder? kcsb = null;\n        resourceBuilder.OnConnectionStringAvailable(async (db, evt, ct) =>\n        {\n            var connectionString = await db.ConnectionStringExpression.GetValueAsync(ct).ConfigureAwait(false) ??\n            throw new DistributedApplicationException($\"ConnectionStringAvailableEvent published for resource '{db.Name}', but the connection string was null.\");\n\n            kcsb = GetConnectionStringBuilder(builder.Resource, connectionString);\n        });\n\n        var healthCheckKey = $\"{kustoDatabase.Name}_check\";\n        resourceBuilder.ApplicationBuilder\n            .Services\n            .AddHealthChecks()\n            .AddAzureKustoHealthCheck(healthCheckKey, isCluster: false, _ => kcsb!);\n\n        resourceBuilder\n            .WithHealthCheck(healthCheckKey);\n\n        return resourceBuilder;\n    }\n\n    /// <summary>\n    /// Configures the Kusto resource to run as an emulator using the Kustainer container.","sourceCodeStart":110,"sourceCodeEnd":146,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.Kusto/AzureKustoBuilderExtensions.cs#L110-L146","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure the parent AddAzureKustoCluster resource is configured so it supplies a connection string to its databases.","If overriding ConnectionString via WithConnectionString/callback, guarantee it returns a non-null value (e.g. a valid Kusto connection string).","Check for model mutations (removing the cluster or clearing its connection string) before the database resource runs.","In tests, do not publish ConnectionStringAvailableEvent manually without setting the connection string."],"exampleFix":"// before\n.WithConnectionString(_ => null) // handler gets null\n\n// after\n.WithConnectionString(\"Data Source=https://mycluster.kusto.windows.net;...\")","handlingStrategy":"type-guard","validationCode":"var cs = await db.ConnectionStringExpression.GetValueAsync(ct);\nif (cs is null)\n    throw new InvalidOperationException(\"Kusto connection string was not populated; check the parent cluster resource configuration.\");","typeGuard":"string? GuardConnectionString(object? value) => value as string is { Length: > 0 } s ? s : null;","tryCatchPattern":"try { await StartAsync(); }\ncatch (DistributedApplicationException ex) when (ex.Message.Contains(\"connection string was null\"))\n{\n    // fix the cluster resource / connection-string override before retry\n}","preventionTips":["Never override ConnectionString with a callback that can return null","Verify the parent Kusto cluster resource is fully configured","In tests, only publish ConnectionStringAvailableEvent after setting the connection string"],"tags":["kusto","azure","connection-string","event"],"backgroundTag":"null-connection-string","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"}