MediatR 8.0 Released
This release brings some (minor) breaking changes to the public API. First, we added a non-generic overload to Send
on IMediator
:
public interface IMediator
{
Task<TResponse> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = default);
+ Task<object> Send(object request, CancellationToken cancellationToken = default);
Task Publish(object notification, CancellationToken cancellationToken = default);
Task Publish<TNotification>(TNotification notification, CancellationToken cancellationToken = default)
where TNotification : INotification;
}
Second, we've modified the Mediator
class to include the CancellationToken
for the virtual PublishCore
method that allows implementors to override the publishing strategy and accept the notification itself:
- protected virtual async Task PublishCore(IEnumerable<Func<Task>> allHandlers)
+ protected virtual async Task PublishCore(IEnumerable<Func<INotification, CancellationToken, Task>> allHandlers, INotification notification, CancellationToken cancellationToken)
{
foreach (var handler in allHandlers)
{
- await handler().ConfigureAwait(false);
+ await handler(notification, cancellationToken).ConfigureAwait(false);
}
}
Finally, a new feature! We've added some new built-in pipeline behaviors for handling exceptions. You can now handle specific exceptions (similar to an Exception Filter in ASP.NET Core):
public interface IRequestExceptionHandler<in TRequest, TResponse, TException>
where TException : Exception
{
Task Handle(TRequest request,
TException exception,
RequestExceptionHandlerState<TResponse> state,
CancellationToken cancellationToken);
}
Alternatively, if you don't want to provide alternate responses based on exceptions, you can provide exception actions:
public interface IRequestExceptionAction<in TRequest, in TException>
where TException : Exception
{
Task Execute(TRequest request, TException exception, CancellationToken cancellationToken);
}
These are a bit simpler if you want to just provide some exception logging. I've also updated the MS DI Extensions package, which registers these new behaviors and interfaces. Enjoy!