microsoft/aspire · error · ArgumentNullException

ArgumentNullException for parameter 'resource' (resource is…

Error message

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

What it means

Constructor null guard in CommandLineArgsCallbackContext: the optional 'resource' argument was explicitly passed as null via the property initializer, which is invalid — the context must carry a non-null resource instance once supplied through that path.

Solutions

  1. Pass the actual IResource (e.g. builder.Resource) to the constructor
  2. Use the two-argument constructor only when intentionally creating a resource-less context and never reading Resource
  3. Null-check the resource variable before constructing

Example fix

// before
var ctx = new CommandLineArgsCallbackContext(args, null);
// after
var ctx = new CommandLineArgsCallbackContext(args, builder.Resource);
Defensive patterns

Strategy: validation

Validate before calling

if (resource is null) resource = builder.Resource;

Type guard

bool HasResource(IResource? r) => r is not null;

Prevention

When it happens

Trigger: new CommandLineArgsCallbackContext(args, null) or the 3-arg overload with a null IResource, e.g. when constructing the context manually in tests or custom callbacks.

Common situations: Custom hosting code building the context before the resource model exists; test fixtures passing null placeholders; refactoring changed which resource variable is passed and it is null.

Related errors


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

Appendix: source

Thrown at src/Aspire.Hosting/ApplicationModel/CommandLineArgsCallbackAnnotation.cs:107

/// <summary>
/// Represents a callback context for the list of command-line arguments associated with an executable resource.
/// </summary>
/// <param name="args"> The list of command-line arguments.</param>
/// <param name="cancellationToken"> The cancellation token associated with this execution.</param>
[AspireExport]
public sealed class CommandLineArgsCallbackContext(IList<object> args, CancellationToken cancellationToken = default)
{
    private readonly IResource? _resource;

    /// <summary>
    /// Represents a callback context for the list of command-line arguments associated with an executable resource.
    /// </summary>
    /// <param name="args"> The list of command-line arguments.</param>
    /// <param name="resource"> The resource associated with this callback context.</param>
    /// <param name="cancellationToken"> The cancellation token associated with this execution.</param>
    public CommandLineArgsCallbackContext(IList<object> args, IResource resource, CancellationToken cancellationToken = default)
        : this(args, cancellationToken) => _resource = resource ?? throw new ArgumentNullException(nameof(resource));

    /// <summary>
    /// Gets the list of command-line arguments.
    /// </summary>
    public IList<object> Args { get; } = args ?? throw new ArgumentNullException(nameof(args));

    /// <summary>
    /// Gets the cancellation token associated with the callback context.
    /// </summary>
    public CancellationToken CancellationToken { get; } = cancellationToken;

    /// <summary>
    /// Gets or sets the execution context for the distributed application.
    /// </summary>
    public DistributedApplicationExecutionContext ExecutionContext { get; init; } = new(DistributedApplicationOperation.Run);

    /// <summary>
    /// Gets or sets the logger for the distributed application.

View on GitHub (pinned to 25830f84bd)