Skip to content

Configuration ​

All configuration goes through the callback of AddAnyMediator:

csharp
services.AddAnyMediator(cfg =>
{
    cfg.RegisterServicesFromAssemblyContaining<Program>();
    cfg.Lifetime = ServiceLifetime.Scoped;
    cfg.AddOpenBehavior(typeof(LoggingBehavior<,>));
    cfg.NotificationPublisherType = typeof(TaskWhenAllPublisher);
});

Calling AddAnyMediator more than once is safe: services and behaviors are not registered twice.

Assemblies ​

MethodScans
RegisterServicesFromAssemblyContaining<T>()the assembly that declares T
RegisterServicesFromAssemblyContaining(Type)the assembly that declares the type
RegisterServicesFromAssembly(Assembly)one assembly
RegisterServicesFromAssemblies(params Assembly[])several assemblies

Options ​

OptionDefaultEffect
LifetimeTransientlifetime of ISender, IPublisher, IMediator, the notification sink, a custom notification publisher, and open behaviors and open processors registered without one; the built-in notification publishers hold no state and are singletons
NotificationPublisherTypeForeachAwaitPublisherhow notification handlers run
NotificationPublishernulla ready instance; wins over NotificationPublisherType
AutoRegisterRequestProcessorsfalsescan for pre- and post-processors
TypeEvaluatorevery typeFunc<Type, bool> that decides which scanned types are registered
RequestExceptionActionProcessorStrategyApplyForUnhandledExceptionswhether exception actions see recovered exceptions

Request, stream and notification handlers, processors, exception handlers and actions found by scanning are always transient.

To leave a type out of scanning, for example a handler that only tests use:

csharp
cfg.TypeEvaluator = type => type.Namespace?.EndsWith(".Testing") != true;

Behaviors ​

MethodPurpose
AddOpenBehavior(Type, ServiceLifetime?)open generic request behavior
AddOpenBehaviors(IEnumerable<Type>, ServiceLifetime?)several, same lifetime
AddOpenBehaviors(IEnumerable<OpenBehavior>)several, each with its own lifetime
AddBehavior<TImplementation>()closed behavior, every interface it implements
AddBehavior<TService, TImplementation>()closed behavior, one service type
AddOpenStreamBehavior, AddOpenStreamBehaviors, AddStreamBehaviorthe same for stream behaviors

Behaviors run in registration order; see Pipeline behaviors.

Processors and exception handling ​

MethodPurpose
AddRequestPreProcessor<T>() / AddRequestPreProcessor<TService, TImplementation>()closed pre-processor
AddOpenRequestPreProcessor(Type)open generic pre-processor for every request
AddRequestPostProcessor<T>() / AddRequestPostProcessor<TService, TImplementation>()closed post-processor
AddOpenRequestPostProcessor(Type)open generic post-processor for every request
AddRequestExceptionHandler<T>()exception handler
AddRequestExceptionAction<T>()exception action

Every method also has an overload that takes Type arguments instead of generic ones.

Services registered ​

ServiceImplementation
ISenderthe request and stream dispatcher
IPublisherthe notification fan-out to sinks
IMediatorboth of the above
INotificationPublisherNotificationPublisherType or NotificationPublisher
INotificationSinkthe in-memory sink that runs your handlers