microsoft/aspire · error · ArgumentException
Application Insights must be omitted, a location string, a…
Error message
Application Insights must be omitted, a location string, a location parameter, or an Application Insights resource builder.
What it means
WithAzureApplicationInsightsForPolyglot accepts a loosely-typed object for Application Insights configuration (omitted, a location string, a location parameter, or a resource builder). The switch expression's discard arm throws ArgumentException when the value's runtime type matches none of the supported shapes.
Solutions
- Pass null to use defaults, a string location like "eastus", an IResourceBuilder<ParameterResource>, or an IResourceBuilder<AzureApplicationInsightsResource>.
- If you have a ParameterResource, pass it through its IResourceBuilder<ParameterResource> wrapper.
- If you have an AzureApplicationInsightsResource, get it from its IResourceBuilder instead of passing the raw resource.
- At the call site, convert/validate the argument's type before invoking the polyglot API.
Example fix
// before env.WithAzureApplicationInsightsForPolyglot(appInsightsResource); // raw resource // after env.WithAzureApplicationInsightsForPolyglot(appInsightsBuilder); // IResourceBuilder<AzureApplicationInsightsResource>
Defensive patterns
Strategy: type-guard
Validate before calling
var ok = applicationInsights is null or string or IResourceBuilder<ParameterResource> or IResourceBuilder<AzureApplicationInsightsResource>;
Type guard
static bool IsValidAppInsightsArg(object? v) => v is null or string or IResourceBuilder<ParameterResource> or IResourceBuilder<AzureApplicationInsightsResource>;
Try / catch
try { env.WithAzureApplicationInsightsForPolyglot(applicationInsights); } catch (ArgumentException ex) { logger.LogError(ex, "Unsupported Application Insights argument type {Type}", applicationInsights?.GetType().Name); } Prevention
- Pass only null, string, IResourceBuilder<ParameterResource>, or IResourceBuilder<AzureApplicationInsightsResource>.
- Wrap raw ParameterResource values in their resource builders before passing.
- In polyglot callers, validate the deserialized type before invoking.
When it happens
Trigger: Passing an unsupported type to WithAzureApplicationInsightsForPolyglot — e.g. an int, a ParameterResource directly (unwrapped by IResourceBuilder<>), an AzureApplicationInsightsResource without its builder wrapper, or an IResourceBuilder of some other resource type.
Common situations: Polyglot (non-C#) callers supplying JSON/YAML values that deserialize to the wrong type; C# callers passing a resource instead of a resource builder; dynamic/reflective wrappers boxing the argument into object with an unexpected concrete 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
- Deployment slot must be a string or a parameter resource…
- Address prefix must be a string or a parameter resource…
- Address prefix must be omitted, a string, or a parameter…
- App Service configuration validation failed. See errors…
- App Service context not found for resource
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/17107370ac9102df.
Report an issue: GitHub.
Appendix: source
Thrown at src/Aspire.Hosting.Azure.AppService/AzureAppServiceEnvironmentExtensions.cs:380
}
/// <summary>
/// Enables Azure Application Insights for the Azure App Service environment
/// </summary>
[AspireExport("withAzureApplicationInsights")]
internal static IResourceBuilder<AzureAppServiceEnvironmentResource> WithAzureApplicationInsightsForPolyglot(
this IResourceBuilder<AzureAppServiceEnvironmentResource> builder,
[AspireUnion(typeof(string), typeof(IResourceBuilder<ParameterResource>), typeof(IResourceBuilder<AzureApplicationInsightsResource>))] object? applicationInsights = null)
{
ArgumentNullException.ThrowIfNull(builder);
return applicationInsights switch
{
null => builder.WithAzureApplicationInsights(),
string applicationInsightsLocation => builder.WithAzureApplicationInsights(applicationInsightsLocation),
IResourceBuilder<ParameterResource> applicationInsightsLocationParameter => builder.WithAzureApplicationInsights(applicationInsightsLocationParameter),
IResourceBuilder<AzureApplicationInsightsResource> applicationInsightsBuilder => builder.WithAzureApplicationInsights(applicationInsightsBuilder),
_ => throw new ArgumentException(
"Application Insights must be omitted, a location string, a location parameter, or an Application Insights resource builder.",
nameof(applicationInsights))
};
}
/// <summary>
/// Configures whether Azure Application Insights should be enabled for the Azure App Service.
/// </summary>
/// <param name="builder">The AzureAppServiceEnvironmentResource to configure.</param>
/// <param name="applicationInsightsLocation">The location for Application Insights.</param>
/// <returns><see cref="IResourceBuilder{T}"/></returns>
[AspireExportIgnore(Reason = "Polyglot AppHosts use the internal withAzureApplicationInsights dispatcher export.")]
public static IResourceBuilder<AzureAppServiceEnvironmentResource> WithAzureApplicationInsights(this IResourceBuilder<AzureAppServiceEnvironmentResource> builder, string applicationInsightsLocation)
{
builder.WithAzureApplicationInsights();
builder.Resource.ApplicationInsightsLocation = applicationInsightsLocation;
return builder;
}View on GitHub (pinned to 25830f84bd)