microsoft/aspire · error · ArgumentNullException

ArgumentNullException for parameter 'callback' (callback is…

Error message

ArgumentNullException for parameter 'callback' (callback is null).

What it means

The ContainerBuildOptionsCallbackAnnotation constructor throws ArgumentNullException when the Func<ContainerBuildOptionsCallbackContext, Task> callback is null. The callback is the whole point of the annotation (it configures container build options during pipeline execution), so a null one is always a programming mistake caught at construction.

Solutions

  1. Pass a valid callback delegate when constructing the annotation.
  2. If the callback is optional in your code, skip creating the annotation entirely instead of passing null.
  3. Guard upstream inputs: ensure the factory/config supplying the delegate is populated before constructing.

Example fix

// before
var annotation = new ContainerBuildOptionsCallbackAnnotation(configuredCallback); // configuredCallback is null

// after
if (configuredCallback is not null)
{
    var annotation = new ContainerBuildOptionsCallbackAnnotation(configuredCallback);
}
Defensive patterns

Strategy: validation

Validate before calling

if (callback is null) throw new InvalidOperationException("ContainerBuildOptions callback must be provided before creating the annotation.");

Type guard

bool IsValid(ContainerBuildOptionsCallbackAnnotation a) => a.Callback is not null;

Try / catch

try { new ContainerBuildOptionsCallbackAnnotation(cb); }
catch (ArgumentNullException ex) { logger.LogError(ex, "Callback delegate was null; check factory/config source"); }

Prevention

When it happens

Trigger: new ContainerBuildOptionsCallbackAnnotation(null); passing a method group that resolves to null (e.g. a nullable delegate field or a method returning null).

Common situations: Building annotations dynamically/reflection-driven code where the callback comes from configuration or an optional parameter left null.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/bec7842cac7a1274. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting/ApplicationModel/ContainerBuildOptionsCallbackAnnotation.cs:20

// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using Aspire.Hosting.Publishing;
using Microsoft.Extensions.Logging;

namespace Aspire.Hosting.ApplicationModel;

/// <summary>
/// Annotation that provides a callback to configure container build options for a resource.
/// </summary>
/// <param name="callback">The callback function to configure container build options.</param>
[Experimental("ASPIREPIPELINES003", UrlFormat = "https://aka.ms/aspire/diagnostics/{0}")]
public sealed class ContainerBuildOptionsCallbackAnnotation(Func<ContainerBuildOptionsCallbackContext, Task> callback) : IResourceAnnotation
{
    /// <summary>
    /// Gets the callback function that will be invoked to configure container build options.
    /// </summary>
    public Func<ContainerBuildOptionsCallbackContext, Task> Callback { get; } = callback ?? throw new ArgumentNullException(nameof(callback));

    /// <summary>
    /// Initializes a new instance of <see cref="ContainerBuildOptionsCallbackAnnotation"/> with a synchronous callback.
    /// </summary>
    /// <param name="callback">The synchronous callback action to configure container build options.</param>
    public ContainerBuildOptionsCallbackAnnotation(Action<ContainerBuildOptionsCallbackContext> callback)
        : this(context =>
        {
            callback(context);
            return Task.CompletedTask;
        })
    {
    }
}

/// <summary>
/// Context for configuring container build options via a callback.
/// </summary>

View on GitHub (pinned to 25830f84bd)