{"record":{"id":"ec44b8c883965395","repo":"microsoft/aspire","slug":"the-connection-string-for-the-resource-resource-name-is-not","errorCode":null,"errorMessage":"The connection string for the resource '{Resource.Name}' is not available.","messagePattern":"The connection string for the resource '(.+?)' is not available\\.","errorType":"exception","errorClass":"DistributedApplicationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/ApplicationModel/ConnectionStringReference.cs","lineNumber":41,"sourceCode":"\n    ValueTask<string?> IValueProvider.GetValueAsync(CancellationToken cancellationToken)\n    {\n        return Resource.GetValueAsync(cancellationToken);\n    }\n\n    async ValueTask<string?> IValueProvider.GetValueAsync(ValueProviderContext context, CancellationToken cancellationToken)\n    {\n        var value = await Resource.GetValueAsync(context, cancellationToken).ConfigureAwait(false);\n\n        if (string.IsNullOrEmpty(value) && !Optional)\n        {\n            ThrowConnectionStringUnavailableException();\n        }\n\n        return value;\n    }\n\n    internal void ThrowConnectionStringUnavailableException() => throw new DistributedApplicationException($\"The connection string for the resource '{Resource.Name}' is not available.\");\n\n}\n","sourceCodeStart":23,"sourceCodeEnd":44,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/ApplicationModel/ConnectionStringReference.cs#L23-L44","documentation":"Aspire throws this DistributedApplicationException when code reads a ConnectionStringReference.Value but the referenced resource's connection string has not been generated yet (or never will be). Connection strings are produced asynchronously during resource provisioning/startup, so reading .Value before the resource has emitted one fails. This is the public accessor's guard in ConnectionStringReference.cs:41.","triggerScenarios":"Accessing ConnectionStringReference.Value before the target resource's connection string is available; reading it in a callback/event that runs before the resource starts; the resource type never publishes a connection string (e.g. a resource without a connection-string contribution such as a container with no endpoint-derived or explicit connection string).","commonSituations":"Developers grab resource.GetConnectionString() / a ConnectionStringReference in BeforeResourceStarted events, in Publish mode where runtime connection strings don't exist, or reference a container/resource that has no connection string configured.","solutions":["Wait for the resource to be running/started before reading the connection string (e.g. use WaitUntil.Running in WaitFor or a ResourceReady/AfterResourceStarted event).","Use the WaitFor completion callbacks (AfterEndpointStarted/ResourceReadyEvent) to access value, where the connection string is guaranteed materialized.","Check the resource actually sets a connection string (ReferenceRelationship to a database/server resource that produces one); if it's a plain container, configure endpoints or an explicit connection string.","If you only need the value lazily, read it inside code executed after startup rather than eagerly at model build time."],"exampleFix":"// before\nvar cs = builder.CreateResourceBuilder(new MyDbResource(\"db\")).Resource.GetConnectionString(); // throws: not started yet\n\n// after\nvar db = builder.AddPostgres(\"pg\").AddDatabase(\"db\");\nvar api = builder.AddProject<Projects.Api>(\"api\")\n    .WaitFor(db); // api's code reads the connection string only after db is running","handlingStrategy":"validation","validationCode":"// Only read the connection string after the resource has started\nif (app.Resource is { } res)\n{\n    var cs = await app.Resource.GetConnectionStringAsync(cancellationToken); // await availability instead of .Value\n}\n// or gate reads behind WaitFor(db) with WaitUntil.Running","typeGuard":"var value = reference.HasContent ? reference.Value : null; // or check resource state == Running before reading","tryCatchPattern":"try { var cs = reference.Value; }\ncatch (DistributedApplicationException ex) { logger.LogWarning(ex, \"Connection string not yet available for {Resource}\", resource.Name); }","preventionTips":["Always use WaitFor(target, WaitUntil.Running) before consuming a target's connection string.","Read connection strings in AfterResourceStarted/ResourceReady callbacks, not in model-build or publish-time code.","Confirm the referenced resource type actually emits a connection string (server/database resources do; bare containers may not)."],"tags":["connection-string","resource-lifecycle","aspire-hosting"],"backgroundTag":"resource-not-found","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"}