{"record":{"id":"17107370ac9102df","repo":"microsoft/aspire","slug":"application-insights-must-be-omitted-a-location-string-a","errorCode":null,"errorMessage":"Application Insights must be omitted, a location string, a location parameter, or an Application Insights resource builder.","messagePattern":"Application Insights must be omitted, a location string, a location parameter, or an Application Insights resource builder\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.AppService/AzureAppServiceEnvironmentExtensions.cs","lineNumber":380,"sourceCode":"    }\n\n    /// <summary>\n    /// Enables Azure Application Insights for the Azure App Service environment\n    /// </summary>\n    [AspireExport(\"withAzureApplicationInsights\")]\n    internal static IResourceBuilder<AzureAppServiceEnvironmentResource> WithAzureApplicationInsightsForPolyglot(\n        this IResourceBuilder<AzureAppServiceEnvironmentResource> builder,\n        [AspireUnion(typeof(string), typeof(IResourceBuilder<ParameterResource>), typeof(IResourceBuilder<AzureApplicationInsightsResource>))] object? applicationInsights = null)\n    {\n        ArgumentNullException.ThrowIfNull(builder);\n\n        return applicationInsights switch\n        {\n            null => builder.WithAzureApplicationInsights(),\n            string applicationInsightsLocation => builder.WithAzureApplicationInsights(applicationInsightsLocation),\n            IResourceBuilder<ParameterResource> applicationInsightsLocationParameter => builder.WithAzureApplicationInsights(applicationInsightsLocationParameter),\n            IResourceBuilder<AzureApplicationInsightsResource> applicationInsightsBuilder => builder.WithAzureApplicationInsights(applicationInsightsBuilder),\n            _ => throw new ArgumentException(\n                \"Application Insights must be omitted, a location string, a location parameter, or an Application Insights resource builder.\",\n                nameof(applicationInsights))\n        };\n    }\n\n    /// <summary>\n    /// Configures whether Azure Application Insights should be enabled for the Azure App Service.\n    /// </summary>\n    /// <param name=\"builder\">The AzureAppServiceEnvironmentResource to configure.</param>\n    /// <param name=\"applicationInsightsLocation\">The location for Application Insights.</param>\n    /// <returns><see cref=\"IResourceBuilder{T}\"/></returns>\n    [AspireExportIgnore(Reason = \"Polyglot AppHosts use the internal withAzureApplicationInsights dispatcher export.\")]\n    public static IResourceBuilder<AzureAppServiceEnvironmentResource> WithAzureApplicationInsights(this IResourceBuilder<AzureAppServiceEnvironmentResource> builder, string applicationInsightsLocation)\n    {\n        builder.WithAzureApplicationInsights();\n        builder.Resource.ApplicationInsightsLocation = applicationInsightsLocation;\n        return builder;\n    }","sourceCodeStart":362,"sourceCodeEnd":398,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.AppService/AzureAppServiceEnvironmentExtensions.cs#L362-L398","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nenv.WithAzureApplicationInsightsForPolyglot(appInsightsResource); // raw resource\n// after\nenv.WithAzureApplicationInsightsForPolyglot(appInsightsBuilder); // IResourceBuilder<AzureApplicationInsightsResource>","handlingStrategy":"type-guard","validationCode":"var ok = applicationInsights is null or string or IResourceBuilder<ParameterResource> or IResourceBuilder<AzureApplicationInsightsResource>;","typeGuard":"static bool IsValidAppInsightsArg(object? v) => v is null or string or IResourceBuilder<ParameterResource> or IResourceBuilder<AzureApplicationInsightsResource>;","tryCatchPattern":"try { env.WithAzureApplicationInsightsForPolyglot(applicationInsights); } catch (ArgumentException ex) { logger.LogError(ex, \"Unsupported Application Insights argument type {Type}\", applicationInsights?.GetType().Name); }","preventionTips":["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."],"tags":["azure","appservice","argument-type","polyglot"],"backgroundTag":"type-mismatch","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"}