{"record":{"id":"e9f1dd875e9b4881","repo":"microsoft/aspire","slug":"resource-is-not-set-this-callback-context-is-not-associated","errorCode":null,"errorMessage":"Resource is not set. This callback context is not associated with a resource.","messagePattern":"Resource is not set\\. This callback context is not associated with a resource\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/ApplicationModel/CommandLineArgsCallbackAnnotation.cs","lineNumber":149,"sourceCode":"    /// </summary>\n    [AspireExport(\"CommandLineArgsCallbackContext.args\", MethodName = \"args\")]\n    internal CommandLineArgsEditor ArgsEditor => new(Args);\n\n    /// <summary>\n    /// Gets the logger facade used by polyglot callbacks.\n    /// </summary>\n    [AspireExport]\n    internal LogFacade Log => new(Logger);\n\n    /// <summary>\n    /// The resource associated with this callback context.\n    /// </summary>\n    /// <remarks>\n    /// This will be set to the resource in all cases where Aspire invokes the callback.\n    /// </remarks>\n    /// <exception cref=\"InvalidOperationException\">Thrown when the CommandLineArgsCallbackContext was created without a specified resource.</exception>\n    [AspireExport]\n    public IResource Resource => _resource ?? throw new InvalidOperationException($\"{nameof(Resource)} is not set. This callback context is not associated with a resource.\");\n\n    /// <summary>\n    /// Gets the execution context associated with this callback.\n    /// </summary>\n    [AspireExport(MethodName = \"executionContext\")]\n    internal DistributedApplicationExecutionContext ExportedExecutionContext => ExecutionContext;\n}\n","sourceCodeStart":131,"sourceCodeEnd":157,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/ApplicationModel/CommandLineArgsCallbackAnnotation.cs#L131-L157","documentation":"The Resource property of CommandLineArgsCallbackContext throws InvalidOperationException when the context was created without a resource. Aspire always sets the resource when it invokes callbacks itself, so this only fires for manually created resource-less contexts.","triggerScenarios":"Creating CommandLineArgsCallbackContext via the 2-arg constructor (args, cancellationToken) and then reading ctx.Resource.","commonSituations":"Unit tests or custom argument pipelines constructing the lightweight context and later code paths (shared helpers) reading Resource unconditionally.","solutions":["Use the 3-arg constructor with the resource when Resource will be read","Guard with _resource != null / try-catch before accessing Resource","Refactor helper code to accept the resource as a parameter instead of reading it from the context"],"exampleFix":"// before\nvar ctx = new CommandLineArgsCallbackContext(args, cancellationToken);\nUseResource(ctx.Resource);\n// after\nvar ctx = new CommandLineArgsCallbackContext(args, resource, cancellationToken);\nUseResource(ctx.Resource);","handlingStrategy":"try-catch","validationCode":"if (context is CommandLineArgsCallbackContext c && typeof(CommandLineArgsCallbackContext).GetField(\"_resource\", BindingFlags.NonPublic | BindingFlags.Instance)!.GetValue(c) is IResource) { /* safe to read Resource */ }","typeGuard":null,"tryCatchPattern":"try { var r = context.Resource; } catch (InvalidOperationException) { /* context created without a resource; use fallback */ }","preventionTips":["Use the 3-arg constructor when any downstream code reads Resource","Rely on Aspire-invoked callbacks, which always set the resource","Audit shared helpers for unconditional Resource access"],"tags":["invalid-operation","callback-context"],"backgroundTag":"invalid-state-transition","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"}