microsoft/aspire · error · ArgumentNullException
Object must be initialized before it can be used
Error message
Object must be initialized before it can be used
What it means
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.
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
Example fix
// before
Action<KubernetesResource>? configure = condition ? Configure : null;
resource.Annotations.Add(new KubernetesServiceCustomizationAnnotation(configure!));
// after
if (condition)
{
resource.Annotations.Add(new KubernetesServiceCustomizationAnnotation(Configure));
} Defensive patterns
Strategy: validation
Validate before calling
if (configure is null) throw new InvalidOperationException("Kubernetes customization action must not be null"); Try / catch
try { resource.Annotations.Add(new KubernetesServiceCustomizationAnnotation(configure)); }
catch (ArgumentNullException) { /* supply a real delegate or skip the annotation */ } Prevention
- Only add the annotation when a real configure action exists
- Use _ => { } for intentional no-op customization
- Avoid nullable delegates flowing into annotation constructors
When it happens
Trigger: 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.
Common situations: Conditional customization code like Action<KubernetesResource> cfg = condition ? Configure : null followed by adding the annotation unconditionally; reflection or DI scenarios supplying a null delegate.
Related errors
- description
- ArgumentNullException for parameter 'callback' (callback is…
- ArgumentNullException for parameter 'resource' (resource is…
- Object value cannot be null (ArgumentNullException…
- Object value cannot be null (ArgumentNullException…
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/ca33e2df63c9413f.
Report an issue: GitHub.
Appendix: source
Thrown at src/Aspire.Hosting.Kubernetes/KubernetesServiceCustomizationAnnotation.cs:20
// The .NET Foundation licenses this file to you under the MIT license.
using Aspire.Hosting.ApplicationModel;
namespace Aspire.Hosting.Kubernetes;
/// <summary>
/// Represents an annotation for customizing a Kubernetes service.
/// </summary>
/// <remarks>
/// Initializes a new instance of the <see cref="KubernetesServiceCustomizationAnnotation"/> class.
/// </remarks>
/// <param name="configure">The configuration action for customizing the Kubernetes service.</param>
public sealed class KubernetesServiceCustomizationAnnotation(Action<KubernetesResource> configure) : IResourceAnnotation
{
/// <summary>
/// Gets the configuration action for customizing the Kubernetes service.
/// </summary>
public Action<KubernetesResource> Configure { get; } = configure ?? throw new ArgumentNullException(nameof(configure));
}
View on GitHub (pinned to 25830f84bd)