{"record":{"id":"ca33e2df63c9413f","repo":"microsoft/aspire","slug":"object-must-be-initialized-before-it-can-be-used","errorCode":null,"errorMessage":"Object must be initialized before it can be used","messagePattern":"Object must be initialized before it can be used","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Kubernetes/KubernetesServiceCustomizationAnnotation.cs","lineNumber":20,"sourceCode":"// The .NET Foundation licenses this file to you under the MIT license.\n\nusing Aspire.Hosting.ApplicationModel;\n\nnamespace Aspire.Hosting.Kubernetes;\n\n/// <summary>\n/// Represents an annotation for customizing a Kubernetes service.\n/// </summary>\n/// <remarks>\n/// Initializes a new instance of the <see cref=\"KubernetesServiceCustomizationAnnotation\"/> class.\n/// </remarks>\n/// <param name=\"configure\">The configuration action for customizing the Kubernetes service.</param>\npublic sealed class KubernetesServiceCustomizationAnnotation(Action<KubernetesResource> configure) : IResourceAnnotation\n{\n    /// <summary>\n    /// Gets the configuration action for customizing the Kubernetes service.\n    /// </summary>\n    public Action<KubernetesResource> Configure { get; } = configure ?? throw new ArgumentNullException(nameof(configure));\n}\n","sourceCodeStart":2,"sourceCodeEnd":22,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Kubernetes/KubernetesServiceCustomizationAnnotation.cs#L2-L22","documentation":"Thrown by the KubernetesServiceCustomizationAnnotation primary constructor when the configure delegate is null. The annotation stores the Action<KubernetesResource> via a null-coalescing ArgumentNullException check because a customization with no action is meaningless.","triggerScenarios":"Calling the annotation constructor (directly or through a WithCustomization-style API) passing a null Action<KubernetesResource>, e.g. passing a local method group that is null or a lambda variable that was never assigned.","commonSituations":"Conditional customization code like Action<KubernetesResource> cfg = condition ? Configure : null followed by adding the annotation unconditionally; reflection or DI scenarios supplying a null delegate.","solutions":["Pass a non-null configure action; if customization is conditional, only add the annotation when the action exists","Replace the null delegate with a no-op: _ => { }","Check upstream API that produced the delegate for accidental null returns"],"exampleFix":"// before\nAction<KubernetesResource>? configure = condition ? Configure : null;\nresource.Annotations.Add(new KubernetesServiceCustomizationAnnotation(configure!));\n// after\nif (condition)\n{\n    resource.Annotations.Add(new KubernetesServiceCustomizationAnnotation(Configure));\n}","handlingStrategy":"validation","validationCode":"if (configure is null) throw new InvalidOperationException(\"Kubernetes customization action must not be null\");","typeGuard":null,"tryCatchPattern":"try { resource.Annotations.Add(new KubernetesServiceCustomizationAnnotation(configure)); }\ncatch (ArgumentNullException) { /* supply a real delegate or skip the annotation */ }","preventionTips":["Only add the annotation when a real configure action exists","Use _ => { } for intentional no-op customization","Avoid nullable delegates flowing into annotation constructors"],"tags":["kubernetes","annotation","null-argument","customization"],"backgroundTag":"null-argument","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"}