{"record":{"id":"91e39c6db472a00b","repo":"microsoft/aspire","slug":"namespace-must-be-a-string-or-a-parameter-resource-builder","errorCode":null,"errorMessage":"Namespace must be a string or a parameter resource builder.","messagePattern":"Namespace must be a string or a parameter resource builder\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Kubernetes/HelmChartOptions.cs","lineNumber":78,"sourceCode":"\n        var expression = ReferenceExpression.Create($\"{@namespace.Resource}\");\n        EnvironmentBuilder.WithAnnotation(new KubernetesNamespaceAnnotation(expression), ResourceAnnotationMutationBehavior.Replace);\n        return this;\n    }\n\n    /// <summary>\n    /// Sets the target Kubernetes namespace for deployment.\n    /// </summary>\n    [AspireExport(MethodName = \"withNamespace\")]\n    internal HelmChartOptions WithNamespace([AspireUnion(typeof(string), typeof(IResourceBuilder<ParameterResource>))] object @namespace)\n    {\n        ArgumentNullException.ThrowIfNull(@namespace);\n\n        return @namespace switch\n        {\n            string namespaceName => WithNamespace(namespaceName),\n            IResourceBuilder<ParameterResource> namespaceParameter => WithNamespace(namespaceParameter),\n            _ => throw new ArgumentException(\"Namespace must be a string or a parameter resource builder.\", nameof(@namespace))\n        };\n    }\n\n    /// <summary>\n    /// Sets the Helm release name for deployment.\n    /// </summary>\n    /// <param name=\"releaseName\">The release name.</param>\n    /// <returns>This <see cref=\"HelmChartOptions\"/> for chaining.</returns>\n    [AspireExportIgnore(Reason = \"Polyglot AppHosts use the union-based withReleaseName dispatcher export.\")]\n    public HelmChartOptions WithReleaseName(string releaseName)\n    {\n        ArgumentException.ThrowIfNullOrWhiteSpace(releaseName);\n        ValidateReleaseName(releaseName, nameof(releaseName));\n\n        var expression = ReferenceExpression.Create($\"{releaseName}\");\n        EnvironmentBuilder.WithAnnotation(new HelmReleaseNameAnnotation(expression), ResourceAnnotationMutationBehavior.Replace);\n        return this;\n    }","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Kubernetes/HelmChartOptions.cs#L60-L96","documentation":"WithNamespace(object) accepts either a string namespace name or an IResourceBuilder<ParameterResource>; anything else is rejected with ArgumentException. The overload exists so callers can pass a static value or a parameter that resolves at publish time, and the switch exhaustively guards the accepted types.","triggerScenarios":"Calling .WithNamespace() with a value whose runtime type is neither string nor IResourceBuilder<ParameterResource> (e.g. an int, a resolvable object, or null-adjacent wrong type passed as object).","commonSituations":"Passing a variable typed as object/dynamic holding something unexpected; passing a parameter value instead of a parameter resource builder; passing a KubernetesNamespace-like wrapper instead of a string.","solutions":["Pass a plain string: .WithNamespace(\"my-namespace\").","Pass a parameter builder created via builder.AddParameter(...): .WithNamespace(paramBuilder).","Convert the value to a string before calling if it is a name-like object."],"exampleFix":"// before\nobject ns = 42;\nhelmOptions.WithNamespace(ns);\n// after\nhelmOptions.WithNamespace(\"my-namespace\");\n// or with a parameter\nvar nsParam = builder.AddParameter(\"namespace\");\nhelmOptions.WithNamespace(nsParam);","handlingStrategy":"type-guard","validationCode":"bool isValidNamespaceArg(object? value) => value is string or IResourceBuilder<ParameterResource>;","typeGuard":"static bool IsNamespaceArg(object? value) => value is string or IResourceBuilder<ParameterResource>;","tryCatchPattern":"try { options.WithNamespace(value); }\ncatch (ArgumentException ex) { logger.LogError(ex, \"Namespace must be a string or IResourceBuilder<ParameterResource>\"); }","preventionTips":["Pass literal strings or builder.AddParameter() results to WithNamespace.","Avoid storing namespace arguments in object/dynamic variables.","Check the XML docs on the overload to confirm accepted types before calling."],"tags":["kubernetes","helm","namespace","argument"],"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"}