microsoft/aspire · error · InvalidOperationException
Resource is not set. This callback context is not…
Error message
Resource is not set. This callback context is not associated with a resource.
What it means
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.
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
Example fix
// before var ctx = new EnvironmentCallbackContext(executionContext); ctx.Resource.Annotations.Add(...); // throws // after var ctx = new EnvironmentCallbackContext(executionContext, resource: myResource); ctx.Resource.Annotations.Add(...);
Defensive patterns
Strategy: type-guard
Validate before calling
// resource presence can be checked via the context's constructor parameters; prefer constructing with a resource: // new EnvironmentCallbackContext(executionContext, resource: resource, cancellationToken: ct)
Type guard
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 Try / catch
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 */ } Prevention
- 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
When it happens
Trigger: 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).
Common situations: 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.
Related errors
- Resource ' ' declares " " debug launch support…
- A circular lifetime reference was detected for resource
- A ConfigureRadiusInfrastructure callback removed or…
- A ConfigureRadiusInfrastructure callback removed port
- A global MCP approval policy cannot be combined with custom…
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/12ba772eaa396f74.
Report an issue: GitHub.
Appendix: source
Thrown at src/Aspire.Hosting/ApplicationModel/EnvironmentCallbackContext.cs:69
/// </summary>
[AspireExport]
internal EnvironmentEditor Environment => new(EnvironmentVariables);
/// <summary>
/// Gets the logger facade used by polyglot callbacks.
/// </summary>
[AspireExport]
internal LogFacade Log => new(() => Logger);
/// <summary>
/// The resource associated with this callback context.
/// </summary>
/// <remarks>
/// This will be set to the resource in all cases where Aspire invokes the callback.
/// </remarks>
/// <exception cref="InvalidOperationException">Thrown when the EnvironmentCallbackContext was created without a specified resource.</exception>
[AspireExport]
public IResource Resource => _resource ?? throw new InvalidOperationException($"{nameof(Resource)} is not set. This callback context is not associated with a resource.");
/// <summary>
/// Gets the execution context associated with this invocation of the AppHost.
/// </summary>
[AspireExport]
public DistributedApplicationExecutionContext ExecutionContext { get; } = executionContext ?? throw new ArgumentNullException(nameof(executionContext));
}
View on GitHub (pinned to 25830f84bd)