microsoft/aspire · error · ArgumentException

Deployment slot must be a string or a parameter resource…

Error message

Deployment slot must be a string or a parameter resource builder.

What it means

WithDeploymentSlotForPolyglot accepts a deployment slot specified either as a string name or as an IResourceBuilder<ParameterResource>. Any other runtime type falls into the switch discard arm and throws ArgumentException, because a slot cannot be resolved from the supplied value.

Solutions

  1. Pass the slot name as a string, e.g. WithDeploymentSlotForPolyglot("staging").
  2. Pass an IResourceBuilder<ParameterResource> obtained from AddParameter if the slot must be parameterized.
  3. Convert/validate the slot argument's type at the call site before invoking the API.
  4. In polyglot configs, quote the slot value so it deserializes as a string, not a number.

Example fix

// before
env.WithDeploymentSlotForPolyglot(2); // numeric
// after
env.WithDeploymentSlotForPolyglot("staging");
Defensive patterns

Strategy: type-guard

Validate before calling

var ok = deploymentSlot is string or IResourceBuilder<ParameterResource>;

Type guard

static bool IsValidSlotArg(object? v) => v is string or IResourceBuilder<ParameterResource>;

Try / catch

try { env.WithDeploymentSlotForPolyglot(deploymentSlot); } catch (ArgumentException ex) { logger.LogError(ex, "Deployment slot must be string or parameter builder, got {Type}", deploymentSlot?.GetType().Name); }

Prevention

When it happens

Trigger: Passing a non-string, non-parameter-builder value — e.g. a numeric slot index, a ParameterResource without its builder wrapper, custom types, or deserialized polyglot config that maps the slot to an unexpected type (object/int).

Common situations: Polyglot (YAML/JSON/JS) configs where the slot value was parsed as a number or object; C# callers passing a raw ParameterResource instead of its IResourceBuilder wrapper; dynamic values boxed as object with an unexpected type.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at src/Aspire.Hosting.Azure.AppService/AzureAppServiceEnvironmentExtensions.cs:459

        return builder;
    }

    /// <summary>
    /// Configures the deployment slot for all Azure App Services in the environment
    /// </summary>
    [AspireExport("withDeploymentSlot")]
    internal static IResourceBuilder<AzureAppServiceEnvironmentResource> WithDeploymentSlotForPolyglot(
        this IResourceBuilder<AzureAppServiceEnvironmentResource> builder,
        [AspireUnion(typeof(string), typeof(IResourceBuilder<ParameterResource>))] object deploymentSlot)
    {
        ArgumentNullException.ThrowIfNull(builder);
        ArgumentNullException.ThrowIfNull(deploymentSlot);

        return deploymentSlot switch
        {
            string deploymentSlotName => builder.WithDeploymentSlot(deploymentSlotName),
            IResourceBuilder<ParameterResource> deploymentSlotParameter => builder.WithDeploymentSlot(deploymentSlotParameter),
            _ => throw new ArgumentException("Deployment slot must be a string or a parameter resource builder.", nameof(deploymentSlot))
        };
    }

    /// <summary>
    /// Configures the slot to which the Azure App Services should be deployed.
    /// </summary>
    /// <param name="builder">The AzureAppServiceEnvironmentResource to configure.</param>
    /// <param name="deploymentSlot">The deployment slot for all App Services in the App Service Environment.</param>
    /// <returns><see cref="IResourceBuilder{T}"/></returns>
    [AspireExportIgnore(Reason = "Polyglot AppHosts use the internal withDeploymentSlot dispatcher export.")]
    public static IResourceBuilder<AzureAppServiceEnvironmentResource> WithDeploymentSlot(this IResourceBuilder<AzureAppServiceEnvironmentResource> builder, string deploymentSlot)
    {
        ArgumentNullException.ThrowIfNull(builder);
        ArgumentException.ThrowIfNullOrWhiteSpace(deploymentSlot);

        builder.Resource.DeploymentSlot = deploymentSlot;
        return builder;
    }

View on GitHub (pinned to 25830f84bd)