Interface IDispatchAmbientScopeAccessor
Exposes the ambient dependency-injection scope for the current logical operation, allowing the singleton dispatch pipeline to resolve scoped message handlers from the correct scope rather than from the root container.
public interface IDispatchAmbientScopeAccessor
- Extension Methods
Remarks
The dispatcher and its message bus are registered as singletons and therefore capture the root IServiceProvider. Resolving a handler that was registered with Scoped (or a handler whose dependencies are scoped) directly from the root container throws InvalidOperationException ("Cannot resolve scoped service '…' from root provider") — the classic captive-dependency failure.
This accessor lets a hosting integration surface the real ambient scope (for example,
HttpContext.RequestServices in ASP.NET Core) so a scoped handler resolves from — and shares
state with — the active request scope. It mirrors the role of
Microsoft.AspNetCore.Http.IHttpContextAccessor: a pull-based ambient accessor with no
dependency on any specific host. When no implementation is registered, or when no ambient scope is
active (for example a background worker), the dispatcher falls back to creating a fresh scope via
IServiceScopeFactory.
Implementations MUST be thread-safe and cheap to query; the property is consulted only when a scoped handler is dispatched, never on the root-resolvable hot path.
Properties
CurrentServiceProvider
Gets the IServiceProvider for the ambient scope of the current operation, or null when no ambient scope is active and the dispatcher should create one.
IServiceProvider? CurrentServiceProvider { get; }
Property Value
- IServiceProvider
The ambient scoped service provider (for example the active request scope), or null if none is available.