{"record":{"id":"14fbb2d00209cab8","repo":"microsoft/aspire","slug":"argumentnullexception-for-parameter-resource-resource-is","errorCode":null,"errorMessage":"ArgumentNullException for parameter 'resource' (resource is null).","messagePattern":"ArgumentNullException for parameter 'resource' \\(resource is null\\)\\.","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/ApplicationModel/CommandLineArgsCallbackAnnotation.cs","lineNumber":107,"sourceCode":"\n/// <summary>\n/// Represents a callback context for the list of command-line arguments associated with an executable resource.\n/// </summary>\n/// <param name=\"args\"> The list of command-line arguments.</param>\n/// <param name=\"cancellationToken\"> The cancellation token associated with this execution.</param>\n[AspireExport]\npublic sealed class CommandLineArgsCallbackContext(IList<object> args, CancellationToken cancellationToken = default)\n{\n    private readonly IResource? _resource;\n\n    /// <summary>\n    /// Represents a callback context for the list of command-line arguments associated with an executable resource.\n    /// </summary>\n    /// <param name=\"args\"> The list of command-line arguments.</param>\n    /// <param name=\"resource\"> The resource associated with this callback context.</param>\n    /// <param name=\"cancellationToken\"> The cancellation token associated with this execution.</param>\n    public CommandLineArgsCallbackContext(IList<object> args, IResource resource, CancellationToken cancellationToken = default)\n        : this(args, cancellationToken) => _resource = resource ?? throw new ArgumentNullException(nameof(resource));\n\n    /// <summary>\n    /// Gets the list of command-line arguments.\n    /// </summary>\n    public IList<object> Args { get; } = args ?? throw new ArgumentNullException(nameof(args));\n\n    /// <summary>\n    /// Gets the cancellation token associated with the callback context.\n    /// </summary>\n    public CancellationToken CancellationToken { get; } = cancellationToken;\n\n    /// <summary>\n    /// Gets or sets the execution context for the distributed application.\n    /// </summary>\n    public DistributedApplicationExecutionContext ExecutionContext { get; init; } = new(DistributedApplicationOperation.Run);\n\n    /// <summary>\n    /// Gets or sets the logger for the distributed application.","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/ApplicationModel/CommandLineArgsCallbackAnnotation.cs#L89-L125","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Pass the actual IResource (e.g. builder.Resource) to the constructor","Use the two-argument constructor only when intentionally creating a resource-less context and never reading Resource","Null-check the resource variable before constructing"],"exampleFix":"// before\nvar ctx = new CommandLineArgsCallbackContext(args, null);\n// after\nvar ctx = new CommandLineArgsCallbackContext(args, builder.Resource);","handlingStrategy":"validation","validationCode":"if (resource is null) resource = builder.Resource;","typeGuard":"bool HasResource(IResource? r) => r is not null;","tryCatchPattern":null,"preventionTips":["Always pass builder.Resource when constructing contexts manually","Prefer the 3-arg constructor when Resource will be accessed","Null-check resource variables in tests"],"tags":["null-argument","callback-context"],"backgroundTag":"null-argument","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}