microsoft/aspire · error · ArgumentException

Resource must be a Cosmos DB, Storage, Container Registry…

Error message

Resource must be a Cosmos DB, Storage, Container Registry, or Key Vault resource builder.

What it means

AddConnectionForPolyglot dispatches to typed AddConnection overloads for Cosmos DB, Storage, Container Registry, and Key Vault resource builders only. Any other resource builder type falls into the default case and raises this ArgumentException naming the offending 'resource' parameter.

Solutions

  1. Pass one of the supported builders: Cosmos DB, Storage, Container Registry, or Key Vault.
  2. For other resource types, create the Foundry connection manually via the lower-level AddConnection(name, configureInfrastructure) API with appropriate connection properties.
  3. Check the ConnectionBuilderExtensions docs for the supported connection resource list.

Example fix

// before
project.AddConnectionForPolyglot(builder.AddRedis("cache")); // throws

// after
project.AddConnectionForPolyglot(builder.AddAzureKeyVault("kv")); // supported type
Defensive patterns

Strategy: type-guard

Validate before calling

bool supported = resource is IResourceBuilder<AzureCosmosDBResource>
    or IResourceBuilder<AzureStorageResource>
    or IResourceBuilder<AzureContainerRegistryResource>
    or IResourceBuilder<AzureKeyVaultResource>;

Type guard

static bool IsSupportedConnectionBuilder(IResourceBuilder<IResource> b) =>
    b is IResourceBuilder<AzureCosmosDBResource>
      or IResourceBuilder<AzureStorageResource>
      or IResourceBuilder<AzureContainerRegistryResource>
      or IResourceBuilder<AzureKeyVaultResource>;

Try / catch

try { project.AddConnectionForPolyglot(resource); } catch (ArgumentException ex) when (ex.Message.Contains("Cosmos DB, Storage, Container Registry, or Key Vault")) { /* use supported resource or manual AddConnection */ }

Prevention

When it happens

Trigger: Calling foundryProject.AddConnectionForPolyglot(builder) with an IResourceBuilder for a resource other than AzureCosmosDBResource, AzureStorageResource, AzureContainerRegistryResource, or AzureKeyVaultResource (e.g. a Redis, SQL, or custom resource builder).

Common situations: Assuming any Azure resource can become a Foundry connection; passing a generic IResourceBuilder<IResource>; typos in the builder variable.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Aspire.Hosting.Foundry/Project/ConnectionBuilderExtensions.cs:356

    internal static IResourceBuilder<AzureCognitiveServicesProjectConnectionResource> AddConnectionForPolyglot(
        this IResourceBuilder<AzureCognitiveServicesProjectResource> builder,
        [AspireUnion(
            typeof(IResourceBuilder<AzureCosmosDBResource>),
            typeof(IResourceBuilder<AzureStorageResource>),
            typeof(IResourceBuilder<AzureContainerRegistryResource>),
            typeof(IResourceBuilder<AzureKeyVaultResource>))]
        object resource)
    {
        ArgumentNullException.ThrowIfNull(builder);
        ArgumentNullException.ThrowIfNull(resource);

        return resource switch
        {
            IResourceBuilder<AzureCosmosDBResource> cosmosDb => builder.AddConnection(cosmosDb),
            IResourceBuilder<AzureStorageResource> storage => builder.AddConnection(storage),
            IResourceBuilder<AzureContainerRegistryResource> registry => builder.AddConnection(registry),
            IResourceBuilder<AzureKeyVaultResource> keyVault => builder.AddConnection(keyVault),
            _ => throw new ArgumentException(
                "Resource must be a Cosmos DB, Storage, Container Registry, or Key Vault resource builder.",
                nameof(resource))
        };
    }

    /// <summary>
    /// Adds a Grounding with Bing Search connection to a Microsoft Foundry project.
    /// </summary>
    /// <remarks>
    /// <para>
    /// The Bing Search resource (<c>Microsoft.Bing/accounts</c>) cannot be provisioned through Aspire
    /// or Bicep. It must be created manually in the
    /// <a href="https://portal.azure.com">Azure portal</a>.
    /// </para>
    /// <para>
    /// Once the Bing resource exists, pass its resource ID to this method. The connection is
    /// created in the Foundry project using API key authentication with
    /// <c>category: "ApiKey"</c> and <c>metadata.Type: "bing_grounding"</c>.

View on GitHub (pinned to 25830f84bd)