{"record":{"id":"9c7145bb8621f816","repo":"microsoft/aspire","slug":"argumentnullexception-for-parameter-resource-resource-is-9c7145","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/ContainerBuildOptionsCallbackAnnotation.cs","lineNumber":58,"sourceCode":"[AspireExport(ExposeProperties = true)]\npublic sealed class ContainerBuildOptionsCallbackContext\n{\n    /// <summary>\n    /// Initializes a new instance of <see cref=\"ContainerBuildOptionsCallbackContext\"/>.\n    /// </summary>\n    /// <param name=\"resource\">The resource being built.</param>\n    /// <param name=\"services\">The service provider.</param>\n    /// <param name=\"logger\">The logger instance.</param>\n    /// <param name=\"cancellationToken\">The cancellation token.</param>\n    /// <param name=\"executionContext\">The distributed application execution context.</param>\n    public ContainerBuildOptionsCallbackContext(\n        IResource resource,\n        IServiceProvider services,\n        ILogger logger,\n        CancellationToken cancellationToken,\n        DistributedApplicationExecutionContext executionContext)\n    {\n        Resource = resource ?? throw new ArgumentNullException(nameof(resource));\n        Services = services ?? throw new ArgumentNullException(nameof(services));\n        Logger = logger ?? throw new ArgumentNullException(nameof(logger));\n        CancellationToken = cancellationToken;\n        ExecutionContext = executionContext ?? throw new ArgumentNullException(nameof(executionContext));\n    }\n\n    /// <summary>\n    /// Gets the resource being built.\n    /// </summary>\n    public IResource Resource { get; }\n\n    /// <summary>\n    /// Gets the service provider.\n    /// </summary>\n    public IServiceProvider Services { get; }\n\n    /// <summary>\n    /// Gets the logger instance.","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/ApplicationModel/ContainerBuildOptionsCallbackAnnotation.cs#L40-L76","documentation":"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.","triggerScenarios":"Constructing ContainerBuildOptionsCallbackContext directly (tests, custom pipeline code) with a null resource argument.","commonSituations":"Unit tests building a fake context where the resource fixture wasn't initialized; custom execution-context code that lost track of the current resource.","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)."],"exampleFix":"// before\nvar ctx = new ContainerBuildOptionsCallbackContext(resource!, services, logger, ct, execCtx); // resource null at runtime\n\n// after\nvar ctx = new ContainerBuildOptionsCallbackContext(new CustomResource(\"mycontainer\"), services, logger, ct, execCtx);","handlingStrategy":"validation","validationCode":"if (resource is null) throw new InvalidOperationException(\"Cannot create ContainerBuildOptionsCallbackContext without a resource.\");","typeGuard":"bool CanBuildContext(IResource? r) => r is not null;","tryCatchPattern":"try { var ctx = new ContainerBuildOptionsCallbackContext(resource, services, logger, ct, execCtx); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"resource\") { logger.LogError(ex, \"Resource missing when building callback context\"); }","preventionTips":["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."],"tags":["argument-null","context","aspire-pipelines"],"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"}