microsoft/aspire · error · ArgumentNullException
Object reference not passed for parameter 'resource'…
Error message
Object reference not passed for parameter 'resource' (ArgumentNullException: resource)
What it means
RustCargoArgsCallbackContext's constructor guards its 'resource' parameter with 'resource ?? throw new ArgumentNullException(nameof(resource))'. The context must always reference the RustAppResource whose cargo arguments are being built, since callbacks read resource annotations and identity from it. A null resource indicates a programming error in whoever constructs the context, so the library fails fast at construction.
Solutions
- Pass the actual RustAppResource instance being configured into the context constructor
- Fix the preceding lookup/builder so it returns a real resource instead of null
- Assert the resource is non-null before constructing the context in test harnesses
Example fix
// before
var context = new RustCargoArgsCallbackContext(resource: null, args, cancellationToken);
// after
var resource = builder.Resource as RustAppResource ?? throw new InvalidOperationException("Not a Rust app resource");
var context = new RustCargoArgsCallbackContext(resource, args, cancellationToken); Defensive patterns
Strategy: type-guard
Validate before calling
if (resource is null) throw new InvalidOperationException("Callback context requires a non-null RustAppResource."); Type guard
bool IsRustAppResource(object? r) => r is RustAppResource;
Try / catch
try { var ctx = new RustCargoArgsCallbackContext(resource, args, ct); } catch (ArgumentNullException ex) when (ex.ParamName == "resource") { log.LogError(ex, "Null resource for callback context"); } Prevention
- Cast and check builder.Resource before constructing the context
- In tests, build the RustAppResource fixture before the context
- Fix lookup helpers to throw meaningful errors instead of returning null
When it happens
Trigger: Constructing RustCargoArgsCallbackContext directly (e.g. in a custom argument pipeline or a unit test harness) and passing null for the resource parameter, such as an uninitialized RustAppResource field or a lookup that returned null.
Common situations: Test code building callback contexts manually; a custom resource builder returning null on a failed lookup; copying sample code where the resource variable was renamed and lost its assignment.
Related errors
- Object reference not passed for parameter 'args'…
- Object reference not passed for parameter 'callback'…
- ArgumentNullException for parameter 'args' (args is null).
- ArgumentNullException for parameter 'callback' (callback is…
- ArgumentNullException for parameter 'resource' (resource is…
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/1efb40e95375c366.
Report an issue: GitHub.
Appendix: source
Thrown at src/Aspire.Hosting.Rust/RustCargoArgsCallbackAnnotation.cs:56
/// <param name="args">The command-line arguments collection.</param>
/// <param name="cancellationToken">The cancellation token associated with this callback context.</param>
/// <remarks>
/// Unlike program arguments, cargo arguments are plain strings rather than <see cref="object"/>.
/// They select build behaviour (<c>--release</c>, <c>--features</c>, <c>--bin</c>) before the program
/// starts, so there is nothing for a deferred value such as an endpoint reference to resolve against;
/// those belong after the <c>--</c> separator and are added with <c>WithArgs</c>.
/// </remarks>
public sealed class RustCargoArgsCallbackContext(RustAppResource resource, IList<string> args, CancellationToken cancellationToken = default)
{
/// <summary>
/// Gets the Rust application resource whose cargo arguments are being built.
/// </summary>
/// <remarks>
/// The same callbacks run for both the local <c>cargo run</c> command line and the generated
/// Dockerfile, so a callback that needs to know which resource it is configuring — or that needs to
/// read annotations placed on it — reads them from here rather than capturing the resource itself.
/// </remarks>
public RustAppResource Resource { get; } = resource ?? throw new ArgumentNullException(nameof(resource));
/// <summary>
/// Gets the list of command-line arguments.
/// </summary>
public IList<string> Args { get; } = args ?? throw new ArgumentNullException(nameof(args));
/// <summary>
/// Gets the cancellation token associated with the callback context.
/// </summary>
public CancellationToken CancellationToken { get; } = cancellationToken;
}
View on GitHub (pinned to 25830f84bd)