{"record":{"id":"b3be9b57d5a56c3f","repo":"microsoft/aspire","slug":"address-prefix-must-be-omitted-a-string-or-a-parameter","errorCode":null,"errorMessage":"Address prefix must be omitted, a string, or a parameter resource builder.","messagePattern":"Address prefix must be omitted, a string, or a parameter resource builder\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.Network/AzureVirtualNetworkExtensions.cs","lineNumber":93,"sourceCode":"\n        return AddAzureVirtualNetworkCore(builder, resource);\n    }\n\n    /// <summary>\n    /// Adds an Azure Virtual Network resource to the application model.\n    /// </summary>\n    [AspireExport(\"addAzureVirtualNetwork\")]\n    internal static IResourceBuilder<AzureVirtualNetworkResource> AddAzureVirtualNetworkForPolyglot(\n        this IDistributedApplicationBuilder builder,\n        [ResourceName] string name,\n        [AspireUnion(typeof(string), typeof(IResourceBuilder<ParameterResource>))] object? addressPrefix = null)\n    {\n        return addressPrefix switch\n        {\n            null => AddAzureVirtualNetwork(builder, name),\n            string addressPrefixValue => AddAzureVirtualNetwork(builder, name, addressPrefixValue),\n            IResourceBuilder<ParameterResource> addressPrefixParameter => AddAzureVirtualNetwork(builder, name, addressPrefixParameter),\n            _ => throw new ArgumentException(\n                \"Address prefix must be omitted, a string, or a parameter resource builder.\",\n                nameof(addressPrefix))\n        };\n    }\n\n    private static IResourceBuilder<AzureVirtualNetworkResource> AddAzureVirtualNetworkCore(\n        IDistributedApplicationBuilder builder,\n        AzureVirtualNetworkResource resource)\n    {\n        if (builder.ExecutionContext.IsRunMode)\n        {\n            // In run mode, we don't want to add the resource to the builder.\n            return builder.CreateResourceBuilder(resource);\n        }\n\n        return builder.AddResource(resource)\n            .WithIconName(\"Router\");\n    }","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.Network/AzureVirtualNetworkExtensions.cs#L75-L111","documentation":"AddAzureVirtualNetworkForPolyglot is the weakly-typed (polyglot) variant of AddAzureVirtualNetwork and accepts the address prefix as null (omit it and let Azure assign), a string CIDR, or an IResourceBuilder<ParameterResource>. It throws this ArgumentException when the argument is any other type, since there is no meaningful conversion. This keeps the string-based polyglot surface type-safe before any Bicep is emitted.","triggerScenarios":"Calling AddAzureVirtualNetworkForPolyglot and passing an object that is not null, string, or IResourceBuilder<ParameterResource> as addressPrefix — e.g. a number/int CIDR, a ParameterResource (unwrapped, not wrapped in a builder), an IResourceBuilder<ContainerResource>, or a deserialized JSON value typed as object.","commonSituations":"Polyglot AppHosts (JavaScript/Python/TypeScript via aspire) where the address prefix arrives as JSON so '10.0.0.0/16' may deserialize to a non-string; passing a raw ParameterResource instead of builder.AddParameter(...) result; copying a typed-C# call signature incorrectly.","solutions":["Pass the address prefix as a string CIDR, e.g. \"10.0.0.0/16\".","Omit/null the argument entirely to let Azure pick the prefix.","Wrap a parameter in a resource builder: builder.AddParameter(\"vnet-prefix\") and pass that IResourceBuilder<ParameterResource>.","If the value comes from JSON/config, coerce it to string before calling (and validate the CIDR format)."],"exampleFix":"// before\nvar vnet = builder.AddAzureVirtualNetworkForPolyglot(\"vnet\", 10.0);\n\n// after\nvar vnet = builder.AddAzureVirtualNetworkForPolyglot(\"vnet\", \"10.0.0.0/16\");","handlingStrategy":"type-guard","validationCode":"bool isValidPrefix(object? v) => v is null or string or IResourceBuilder<ParameterResource>;","typeGuard":"bool IsAddressPrefixAccepted(object? v) => v is null or string or IResourceBuilder<ParameterResource>;","tryCatchPattern":"try { builder.AddAzureVirtualNetworkForPolyglot(name, rawPrefix); }\ncatch (ArgumentException ex) when (ex.ParamName == \"addressPrefix\") { /* coerce rawPrefix to string or null and retry */ }","preventionTips":["Always pass CIDRs as strings or builder.AddParameter results in polyglot hosting code.","Validate the value's runtime type at the JSON/config boundary before calling hosting APIs.","Prefer the strongly-typed C# overloads when building AppHosts in C#."],"tags":["azure","networking","argument-validation","polyglot"],"backgroundTag":"invalid-argument-value","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"}