{"record":{"id":"3f3a449acbd619a9","repo":"microsoft/aspire","slug":"address-prefix-must-be-a-string-or-a-parameter-resource","errorCode":null,"errorMessage":"Address prefix must be a string or a parameter resource builder.","messagePattern":"Address prefix must be a string or a parameter resource builder\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Azure.Network/AzureVirtualNetworkExtensions.cs","lineNumber":259,"sourceCode":"    }\n\n    /// <summary>\n    /// Adds an Azure subnet resource to an Azure Virtual Network resource.\n    /// </summary>\n    [AspireExport(\"addSubnet\")]\n    internal static IResourceBuilder<AzureSubnetResource> AddSubnetForPolyglot(\n        this IResourceBuilder<AzureVirtualNetworkResource> builder,\n        [ResourceName] string name,\n        [AspireUnion(typeof(string), typeof(IResourceBuilder<ParameterResource>))] object addressPrefix,\n        string? subnetName = null)\n    {\n        ArgumentNullException.ThrowIfNull(addressPrefix);\n\n        return addressPrefix switch\n        {\n            string addressPrefixValue => AddSubnet(builder, name, addressPrefixValue, subnetName),\n            IResourceBuilder<ParameterResource> addressPrefixParameter => AddSubnet(builder, name, addressPrefixParameter, subnetName),\n            _ => throw new ArgumentException(\n                \"Address prefix must be a string or a parameter resource builder.\",\n                nameof(addressPrefix))\n        };\n    }\n\n    private static IResourceBuilder<AzureSubnetResource> AddSubnetCore(\n        IResourceBuilder<AzureVirtualNetworkResource> builder,\n        AzureSubnetResource subnet)\n    {\n        builder.Resource.Subnets.Add(subnet);\n\n        if (builder.ApplicationBuilder.ExecutionContext.IsRunMode)\n        {\n            // In run mode, we don't want to add the resource to the builder.\n            return builder.ApplicationBuilder.CreateResourceBuilder(subnet);\n        }\n\n        return builder.ApplicationBuilder.AddResource(subnet)","sourceCodeStart":241,"sourceCodeEnd":277,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Azure.Network/AzureVirtualNetworkExtensions.cs#L241-L277","documentation":"AddSubnetForPolyglot requires the subnet addressPrefix to be either a string CIDR or an IResourceBuilder<ParameterResource> (null is not allowed here, unlike AddAzureVirtualNetworkForPolyglot, per the leading ThrowIfNull). Any other type throws this ArgumentException because the method cannot build a Bicep expression from it.","triggerScenarios":"Calling AddSubnetForPolyglot with addressPrefix as an int, double, unwrapped ParameterResource, arbitrary object from JSON deserialization, or a builder of the wrong resource type; also passing null.","commonSituations":"Polyglot scenarios where the subnet prefix is read from a JSON config and arrives typed as object/number; passing the result of builder.AddParameter without noting it is already an IResourceBuilder<ParameterResource> (that one is valid — the failures are the other shapes); confusing subnet CIDR size expectations.","solutions":["Pass the prefix as a string CIDR sized for a subnet, e.g. \"10.0.1.0/24\".","Pass an IResourceBuilder<ParameterResource> from builder.AddParameter(...) to make it configurable.","Ensure the value is non-null — this overload does not accept null.","If the value is untyped (e.g. from JSON), convert with value?.ToString() after validating it is a CIDR string."],"exampleFix":"// before\nvar subnet = vnet.AddSubnetForPolyglot(\"subnet-a\", 24);\n\n// after\nvar subnet = vnet.AddSubnetForPolyglot(\"subnet-a\", \"10.0.1.0/24\");","handlingStrategy":"type-guard","validationCode":"if (addressPrefix is null) throw new InvalidOperationException(\"Subnet addressPrefix is required\");\nbool ok = addressPrefix is string or IResourceBuilder<ParameterResource>;","typeGuard":"bool IsSubnetPrefixAccepted(object v) => v is string or IResourceBuilder<ParameterResource>;","tryCatchPattern":"try { vnet.AddSubnetForPolyglot(name, rawPrefix); }\ncatch (ArgumentException ex) when (ex.ParamName == \"addressPrefix\") { /* supply a string CIDR and retry */ }","preventionTips":["Remember null is not accepted for subnet prefixes — supply a string CIDR.","Normalize JSON-derived values to strings before calling polyglot APIs.","Use builder.AddParameter for environment-specific prefixes."],"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"}