{"record":{"id":"12ba772eaa396f74","repo":"microsoft/aspire","slug":"resource-is-not-set-this-callback-context-is-not-associated-12ba77","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/EnvironmentCallbackContext.cs","lineNumber":69,"sourceCode":"    /// </summary>\n    [AspireExport]\n    internal EnvironmentEditor Environment => new(EnvironmentVariables);\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 EnvironmentCallbackContext 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 invocation of the AppHost.\n    /// </summary>\n    [AspireExport]\n    public DistributedApplicationExecutionContext ExecutionContext { get; } = executionContext ?? throw new ArgumentNullException(nameof(executionContext));\n}\n","sourceCodeStart":51,"sourceCodeEnd":77,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/ApplicationModel/EnvironmentCallbackContext.cs#L51-L77","documentation":"EnvironmentCallbackContext.Resource is optional at construction but many callbacks require the resource. Accessing Resource when the context was created without one throws this InvalidOperationException, since the context is then not associated with any resource.","triggerScenarios":"Calling ctx.Resource inside an EnvironmentCallback that was registered globally or invoked with a context constructed via the constructor overload without a resource (e.g., manually created contexts in tests or shared/global environment callbacks).","commonSituations":"Manually constructing EnvironmentCallbackContext in unit tests without passing the resource; callbacks added via ConfigureAllRetrievedEnvironment-style APIs where no single resource exists but code still touches Resource.","solutions":["Pass the IResource when constructing EnvironmentCallbackContext so Resource is set","In shared/global callbacks, avoid accessing Resource, or guard with a null-check on the underlying _resource before use","In tests, always supply a concrete resource (e.g., a GeneratedResource) when building the context"],"exampleFix":"// before\nvar ctx = new EnvironmentCallbackContext(executionContext);\nctx.Resource.Annotations.Add(...); // throws\n// after\nvar ctx = new EnvironmentCallbackContext(executionContext, resource: myResource);\nctx.Resource.Annotations.Add(...);","handlingStrategy":"type-guard","validationCode":"// resource presence can be checked via the context's constructor parameters; prefer constructing with a resource:\n// new EnvironmentCallbackContext(executionContext, resource: resource, cancellationToken: ct)","typeGuard":"bool TryGetResource(EnvironmentCallbackContext ctx, out IResource? resource) => (resource = ctx is { } c && c.ToString() is not null ? null : null) is not null; // Resource getter throws by design; construct with a resource instead","tryCatchPattern":"try { var res = context.Resource; } catch (InvalidOperationException ex) when (ex.Message.Contains(\"Resource is not set\")) { /* this context has no resource; skip resource-specific work */ }","preventionTips":["Always construct EnvironmentCallbackContext with the resource parameter","In shared/global callbacks, do not access Resource","In unit tests, pass a real or generated resource when building contexts"],"tags":["aspire","callbacks","resources"],"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"}