{"record":{"id":"080e7154a6071144","repo":"microsoft/aspire","slug":"named-service-references-are-not-supported-for-azure","errorCode":null,"errorMessage":"Named service references are not supported for Azure Functions resources.","messagePattern":"Named service references are not supported for Azure Functions resources\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.Functions/AzureFunctionsProjectResourceExtensions.cs","lineNumber":376,"sourceCode":"        IResourceBuilder<AzureFunctionsProjectResource> destination,\n        IResourceBuilder<IResource> source,\n        string? connectionName,\n        bool optional,\n        string? name)\n    {\n        if (source.Resource is not IResourceWithConnectionString || source.Resource is not IResourceWithAzureFunctionsConfig azureFunctionsConfig)\n        {\n            return null;\n        }\n\n        if (optional)\n        {\n            throw new InvalidOperationException(\"Optional references are not supported for Azure Functions resources.\");\n        }\n\n        if (name is not null)\n        {\n            throw new InvalidOperationException(\"Named service references are not supported for Azure Functions resources.\");\n        }\n\n        destination.WithReferenceRelationship(source.Resource);\n\n        return destination.WithEnvironment(context =>\n        {\n            connectionName ??= source.Resource.Name;\n            azureFunctionsConfig.ApplyAzureFunctionsConfiguration(context.EnvironmentVariables, connectionName);\n        });\n    }\n\n    private static string CreateDefaultStorageName(this IDistributedApplicationBuilder builder)\n    {\n        // Use ProjectNameSha256 for stable naming across deployments regardless of path\n        var applicationHash = builder.Configuration[\"AppHost:ProjectNameSha256\"]![..5].ToLowerInvariant();\n        return $\"{DefaultAzureFunctionsHostStorageName}{applicationHash}\";\n    }\n","sourceCodeStart":358,"sourceCodeEnd":394,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.Functions/AzureFunctionsProjectResourceExtensions.cs#L358-L394","documentation":"TryWithReference in the Azure Functions extensions rejects named service references: when a non-null name is supplied along with the reference, it throws this InvalidOperationException. Functions projects derive their config keys from the referenced resource, and custom names would break that mapping.","triggerScenarios":"Calling WithReference(destination, source, connectionName: \"myname\") (or equivalent named overload) on an Azure Functions project resource.","commonSituations":"Providing an explicit connection/environment-variable name as you would for a normal project; migrating reference code from a WebApp to a Functions app while keeping the name argument.","solutions":["Drop the name argument and call the unnamed WithReference overload; Functions derives the config key automatically.","If a specific name is required, create a wrapper resource or use environment variables directly via WithEnvironment.","Adjust shared wiring helpers to not pass names for Functions destinations."],"exampleFix":"// before\nfunctionsProject.WithReference(cache, \"RedisConnection\");\n\n// after\nfunctionsProject.WithReference(cache); // key is derived from the resource","handlingStrategy":"validation","validationCode":"if (name is not null && destination.Resource is AzureFunctionsProjectResource) throw new InvalidOperationException(\"Do not pass a connection name to WithReference on Azure Functions resources.\");","typeGuard":"static bool SupportsNamedReference(IResourceBuilder<IResource> b) => b.Resource is not AzureFunctionsProjectResource;","tryCatchPattern":"try { functionsProject.WithReference(dep, \"MyConn\"); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"Named service references\")) { /* retry without name */ }","preventionTips":["Use the unnamed WithReference overload for Functions projects.","Let Functions derive config keys from the resource name.","Audit shared helper methods for name arguments."],"tags":["aspire","azure-functions","withreference","named-references"],"backgroundTag":"unsupported-operation","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"}