Class MediatorServiceCollectionExtensions
- Namespace
- Microsoft.Extensions.DependencyInjection
- Assembly
- Stratara.Mediator.dll
DI extensions that register the Stratara mediator + pipeline behaviors + handler discovery.
public static class MediatorServiceCollectionExtensions
- Inheritance
-
MediatorServiceCollectionExtensions
- Inherited Members
Methods
AddCommandHandlersFromAssemblyContaining<T>(IServiceCollection)
Scan the assembly containing T and register every concrete
ICommandHandler<TRequest> implementation as a scoped service.
public static IServiceCollection AddCommandHandlersFromAssemblyContaining<T>(this IServiceCollection services)
Parameters
servicesIServiceCollectionThe service collection to mutate.
Returns
- IServiceCollection
The same service collection, to enable chaining.
Type Parameters
TA marker type from the assembly to scan. Typically
Programor the application's marker interface.
Examples
Register every command handler in the host's assembly via a marker interface so domain assemblies are scanned without referencing concrete types here:
public interface IAppMarker { }
builder.Services.AddCommandHandlersFromAssemblyContaining<IAppMarker>();
AddMediator(IServiceCollection)
Register IMediator as a scoped service.
public static IServiceCollection AddMediator(this IServiceCollection services)
Parameters
servicesIServiceCollectionThe service collection to mutate.
Returns
- IServiceCollection
The same service collection, to enable chaining.
Examples
Register the mediator alongside handlers discovered in the host's assembly:
var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddMediator()
.AddCommandHandlersFromAssemblyContaining<Program>()
.AddQueryHandlersFromAssemblyContaining<Program>();
Remarks
Idempotent — calling multiple times appends duplicate registrations; DI resolves the last one. Use AddAuthorizingMediator<TAuthorizationProvider>(IServiceCollection) instead if you want the authorizing decorator chain.
Every dispatch is traced. A OpenTelemetry.Trace.Tracer the host has registered — before or after this call — is the one the mediator uses. Where the host registers none, a fallback is registered that emits the dispatch spans from the framework's own activity source, SourceName, so a host that subscribes to framework telemetry sees them and a host that subscribes to nothing pays for nothing. No telemetry registration is required for the mediator to start.
This method also wires a startup-time validator that fails fast if the host registers
[RequireRole]-annotated request types without also pointing IMediator
at an IAuthorizingMediator. Custom decorators that wrap
Stratara.Mediator.Authorization.AuthorizingMediator must implement
IAuthorizingMediator on their outermost layer so the validator
recognises them.
AddPipelineBehavior(IServiceCollection, Type)
Register an open-generic IPipelineBehavior<TRequest> implementation for void commands.
public static IServiceCollection AddPipelineBehavior(this IServiceCollection services, Type openGenericBehaviorType)
Parameters
servicesIServiceCollectionThe service collection to mutate.
openGenericBehaviorTypeTypeAn open generic type definition with one type parameter.
Returns
- IServiceCollection
The same service collection, to enable chaining.
Examples
Behaviours run in registration order, outermost first:
services.AddPipelineBehavior(typeof(LoggingBehavior<>));
Remarks
The provided type must be a one-parameter open generic
(e.g. typeof(LoggingBehavior<>)). Behaviors are resolved per-request in DI
registration order — first registered runs outermost. Registering the same behavior type
more than once installs it once, so a host that composes overlapping service bundles does
not run the behavior twice.
Exceptions
- ArgumentNullException
openGenericBehaviorTypeis null.- ArgumentException
openGenericBehaviorTypeis not an open generic with one type parameter.
AddPipelineBehaviorWithResult(IServiceCollection, Type)
Register an open-generic IPipelineBehavior<TRequest, TResult> implementation.
public static IServiceCollection AddPipelineBehaviorWithResult(this IServiceCollection services, Type openGenericBehaviorType)
Parameters
servicesIServiceCollectionThe service collection to mutate.
openGenericBehaviorTypeTypeAn open generic type definition with two type parameters.
Returns
- IServiceCollection
The same service collection, to enable chaining.
Examples
The result-returning request shape needs its own registration — a behaviour registered only
through AddPipelineBehavior never sees a query:
services.AddPipelineBehaviorWithResult(typeof(LoggingBehavior<,>));
Remarks
The provided type must be a two-parameter open generic
(e.g. typeof(LoggingBehavior<,>)). Behaviors are resolved per-request in DI
registration order — first registered runs outermost. Registering the same behavior type
more than once installs it once, so a host that composes overlapping service bundles does
not run the behavior twice.
Exceptions
- ArgumentNullException
openGenericBehaviorTypeis null.- ArgumentException
openGenericBehaviorTypeis not an open generic with two type parameters.
AddQueryHandlersFromAssemblyContaining<T>(IServiceCollection)
Scan the assembly containing T and register every concrete
IQueryHandler<TRequest, TResult> implementation as a scoped service.
public static IServiceCollection AddQueryHandlersFromAssemblyContaining<T>(this IServiceCollection services)
Parameters
servicesIServiceCollectionThe service collection to mutate.
Returns
- IServiceCollection
The same service collection, to enable chaining.
Type Parameters
TA marker type from the assembly to scan. Typically
Programor the application's marker interface.
Examples
Register every query handler in the host's assembly. The same call also picks up
handlers for ICommand<TResult> because both share the
IQueryHandler<TRequest, TResult> contract.
builder.Services.AddQueryHandlersFromAssemblyContaining<IAppMarker>();