{"record":{"id":"d379d09350fd7400","repo":"microsoft/aspire","slug":"resource-resourcename-does-not-expose-a-connection-string","errorCode":null,"errorMessage":"Resource '{resourceName}' does not expose a connection string.","messagePattern":"Resource '(.+?)' does not expose a connection string\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Testing/DistributedApplicationHostingTestingExtensions.cs","lineNumber":179,"sourceCode":"    /// <summary>\n    /// Gets the connection string for the specified resource.\n    /// </summary>\n    /// <param name=\"app\">The application.</param>\n    /// <param name=\"resourceName\">The resource name.</param>\n    /// <param name=\"cancellationToken\">A <see cref=\"CancellationToken\"/>.</param>\n    /// <remarks>This overload is not available in polyglot app hosts. Use the exported overload without a cancellation token instead.</remarks>\n    /// <returns>The connection string for the specified resource.</returns>\n    /// <exception cref=\"ArgumentException\">The resource was not found or does not expose a connection string.</exception>\n    [AspireExportIgnore(Reason = \"Use the exported getConnectionString overload without a cancellation token.\")]\n    public static ValueTask<string?> GetConnectionStringAsync(this DistributedApplication app, string resourceName, CancellationToken cancellationToken = default)\n    {\n        ArgumentNullException.ThrowIfNull(app);\n        ArgumentException.ThrowIfNullOrEmpty(resourceName);\n\n        var resource = GetResource(app, resourceName);\n        if (resource is not IResourceWithConnectionString resourceWithConnectionString)\n        {\n            throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, Properties.Resources.ResourceDoesNotExposeConnectionStringExceptionMessage, resourceName), nameof(resourceName));\n        }\n\n        return resourceWithConnectionString.GetConnectionStringAsync(cancellationToken);\n    }\n\n    /// <summary>\n    /// Gets the connection string for the specified resource.\n    /// </summary>\n    /// <param name=\"app\">The application.</param>\n    /// <param name=\"resourceName\">The resource name.</param>\n    /// <returns>The connection string for the specified resource.</returns>\n    /// <exception cref=\"ArgumentException\">The resource was not found or does not expose a connection string.</exception>\n    [AspireExport(\"getConnectionString\")]\n    internal static Task<string?> GetConnectionStringAsyncExport(this DistributedApplication app, string resourceName)\n    {\n        return app.GetConnectionStringAsync(resourceName, default).AsTask();\n    }\n","sourceCodeStart":161,"sourceCodeEnd":197,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Testing/DistributedApplicationHostingTestingExtensions.cs#L161-L197","documentation":"GetConnectionStringAsync throws this ArgumentException when the named resource exists in the application model but does not implement IResourceWithConnectionString, so it has no connection string to expose. The library only supports retrieving connection strings from resources that declare one.","triggerScenarios":"Calling app.GetConnectionStringAsync(appName, \"resourceName\") for a resource type without a connection string — e.g. a project resource, executable, or a container without connection-string annotations.","commonSituations":"Passing a project/frontend resource name instead of the database resource; using a custom resource type that doesn't implement IResourceWithConnectionString; typos mapping to the wrong resource.","solutions":["Pass the name of the resource that actually owns the connection string (e.g. the postgres/sqlserver resource, not the consuming project)","If it's a custom resource, implement IResourceWithConnectionString on it","Use GetResource to inspect the resource type before calling GetConnectionStringAsync"],"exampleFix":"// before\nvar cs = await app.GetConnectionStringAsync(\"frontend\"); // project resource\n// after\nvar cs = await app.GetConnectionStringAsync(\"postgres\"); // resource with connection string","handlingStrategy":"type-guard","validationCode":"var resource = app.GetResource(name);\nbool hasCs = resource is IResourceWithConnectionString;","typeGuard":"static bool ExposesConnectionString(DistributedApplication app, string name) =>\n    app.GetResource(name) is IResourceWithConnectionString;","tryCatchPattern":"try { var cs = await app.GetConnectionStringAsync(name); }\ncatch (ArgumentException ex) when (ex.ParamName == \"resourceName\")\n{\n    // resource has no connection string; use GetEndpoint or a different resource\n}","preventionTips":["Only call GetConnectionStringAsync on database/cache-style resources","Confirm the AppHost resource implements IResourceWithConnectionString","Match resource names exactly as declared in the AppHost"],"tags":["aspire","testing","connection-string","argument"],"backgroundTag":"invalid-argument-value","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"}