Class ResourceAuthorizationBehavior<TMessage, TResource, TResponse>
Pipeline behavior that loads a resource and performs resource-based authorization before the handler runs. Registered as scoped so the injected IServiceProvider is the request-scoped provider, allowing correct resolution of scoped dependencies.
public sealed class ResourceAuthorizationBehavior<TMessage, TResource, TResponse> : IPipelineBehavior<TMessage, TResponse> where TMessage : IAuthorizeResource<TResource>, IMessage where TResponse : IResult, IFailureFactory<TResponse>
Type Parameters
TMessageThe message type, constrained to IAuthorizeResource<TResource>.
TResourceThe resource type loaded for authorization.
TResponseThe response type, constrained to IResult and IFailureFactory<TSelf>.
- Inheritance
-
ResourceAuthorizationBehavior<TMessage, TResource, TResponse>
- Implements
-
IPipelineBehavior<TMessage, TResponse>
- Inherited Members
- Extension Methods
Remarks
This behavior cannot be registered as an open generic because it has 3 type parameters while Mediator.IPipelineBehavior<TMessage, TResponse> has 2. Register per-command via AddResourceAuthorization<TMessage, TResource, TResponse>(IServiceCollection).
The behavior is registered as scoped (not singleton) because it resolves
IResourceLoader<TMessage, TResource> from the injected IServiceProvider.
A singleton would receive the root provider, causing InvalidOperationException
when ASP.NET Core's scope validation is enabled (default in Development).
Pipeline execution order for a command implementing both IAuthorize and IAuthorizeResource<TResource>:
- AuthorizationBehavior — checks static permissions
- ResourceAuthorizationBehavior — loads resource, checks ownership
- ValidationBehavior — validates command properties
- Handler — pure business logic
Constructors
ResourceAuthorizationBehavior(IActorProvider, IServiceProvider)
Pipeline behavior that loads a resource and performs resource-based authorization before the handler runs. Registered as scoped so the injected IServiceProvider is the request-scoped provider, allowing correct resolution of scoped dependencies.
public ResourceAuthorizationBehavior(IActorProvider actorProvider, IServiceProvider serviceProvider)
Parameters
actorProviderIActorProviderserviceProviderIServiceProvider
Remarks
This behavior cannot be registered as an open generic because it has 3 type parameters while Mediator.IPipelineBehavior<TMessage, TResponse> has 2. Register per-command via AddResourceAuthorization<TMessage, TResource, TResponse>(IServiceCollection).
The behavior is registered as scoped (not singleton) because it resolves
IResourceLoader<TMessage, TResource> from the injected IServiceProvider.
A singleton would receive the root provider, causing InvalidOperationException
when ASP.NET Core's scope validation is enabled (default in Development).
Pipeline execution order for a command implementing both IAuthorize and IAuthorizeResource<TResource>:
- AuthorizationBehavior — checks static permissions
- ResourceAuthorizationBehavior — loads resource, checks ownership
- ValidationBehavior — validates command properties
- Handler — pure business logic
Methods
Handle(TMessage, MessageHandlerDelegate<TMessage, TResponse>, CancellationToken)
public ValueTask<TResponse> Handle(TMessage message, MessageHandlerDelegate<TMessage, TResponse> next, CancellationToken cancellationToken)
Parameters
messageTMessagenextMessageHandlerDelegate<TMessage, TResponse>cancellationTokenCancellationToken
Returns
- ValueTask<TResponse>