microsoft/aspire · error · ArgumentNullException
ArgumentNullException for parameter 'resource' (resource is…
Error message
ArgumentNullException for parameter 'resource' (resource is null).
What it means
ContainerBuildOptionsCallbackContext's constructor validates that the IResource it is created for is not null. The context carries the resource whose container build options are being configured; without a resource the context is meaningless, so the constructor throws ArgumentNullException.
Solutions
- Pass a valid IResource instance (e.g. the resource the annotation is attached to).
- In tests, initialize the resource before creating the context (e.g. new MyResource("name")).
- Check earlier code for a null resource flowing through (nullable annotations should flag it).
Example fix
// before
var ctx = new ContainerBuildOptionsCallbackContext(resource!, services, logger, ct, execCtx); // resource null at runtime
// after
var ctx = new ContainerBuildOptionsCallbackContext(new CustomResource("mycontainer"), services, logger, ct, execCtx); Defensive patterns
Strategy: validation
Validate before calling
if (resource is null) throw new InvalidOperationException("Cannot create ContainerBuildOptionsCallbackContext without a resource."); Type guard
bool CanBuildContext(IResource? r) => r is not null;
Try / catch
try { var ctx = new ContainerBuildOptionsCallbackContext(resource, services, logger, ct, execCtx); }
catch (ArgumentNullException ex) when (ex.ParamName == "resource") { logger.LogError(ex, "Resource missing when building callback context"); } Prevention
- Pass the resource from the annotation/execution context rather than a separately tracked variable.
- In tests, construct a concrete resource instance before building the context.
- Enable nullable reference types so a null resource is flagged at compile time.
When it happens
Trigger: Constructing ContainerBuildOptionsCallbackContext directly (tests, custom pipeline code) with a null resource argument.
Common situations: Unit tests building a fake context where the resource fixture wasn't initialized; custom execution-context code that lost track of the current resource.
Related errors
- ArgumentNullException for parameter 'callback' (callback is…
- ArgumentNullException for parameter 'services' (services is…
- ArgumentNullException: hostBuilder
- innerResource
- Must not be null
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/9c7145bb8621f816.
Report an issue: GitHub.
Appendix: source
Thrown at src/Aspire.Hosting/ApplicationModel/ContainerBuildOptionsCallbackAnnotation.cs:58
[AspireExport(ExposeProperties = true)]
public sealed class ContainerBuildOptionsCallbackContext
{
/// <summary>
/// Initializes a new instance of <see cref="ContainerBuildOptionsCallbackContext"/>.
/// </summary>
/// <param name="resource">The resource being built.</param>
/// <param name="services">The service provider.</param>
/// <param name="logger">The logger instance.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <param name="executionContext">The distributed application execution context.</param>
public ContainerBuildOptionsCallbackContext(
IResource resource,
IServiceProvider services,
ILogger logger,
CancellationToken cancellationToken,
DistributedApplicationExecutionContext executionContext)
{
Resource = resource ?? throw new ArgumentNullException(nameof(resource));
Services = services ?? throw new ArgumentNullException(nameof(services));
Logger = logger ?? throw new ArgumentNullException(nameof(logger));
CancellationToken = cancellationToken;
ExecutionContext = executionContext ?? throw new ArgumentNullException(nameof(executionContext));
}
/// <summary>
/// Gets the resource being built.
/// </summary>
public IResource Resource { get; }
/// <summary>
/// Gets the service provider.
/// </summary>
public IServiceProvider Services { get; }
/// <summary>
/// Gets the logger instance.View on GitHub (pinned to 25830f84bd)