diff --git a/src/MediatR/IPipelineBehavior.cs b/src/MediatR/IPipelineBehavior.cs index c4abfbc8..f2012555 100644 --- a/src/MediatR/IPipelineBehavior.cs +++ b/src/MediatR/IPipelineBehavior.cs @@ -23,8 +23,8 @@ public interface IPipelineBehavior where TRequest : IReq /// Pipeline handler. Perform any additional behavior and await the delegate as necessary /// /// Incoming request - /// Cancellation token /// Awaitable delegate for the next action in the pipeline. Eventually this delegate represents the handler. + /// Cancellation token /// Awaitable task returning the - Task Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate next); + Task Handle(TRequest request, RequestHandlerDelegate next, CancellationToken cancellationToken); } \ No newline at end of file diff --git a/src/MediatR/IStreamPipelineBehavior.cs b/src/MediatR/IStreamPipelineBehavior.cs index 32ce6ee5..f553b6fa 100644 --- a/src/MediatR/IStreamPipelineBehavior.cs +++ b/src/MediatR/IStreamPipelineBehavior.cs @@ -24,8 +24,8 @@ public interface IStreamPipelineBehavior /// Stream Pipeline handler. Perform any additional behavior and iterate the delegate as necessary /// /// Incoming request - /// Cancellation token /// Awaitable delegate for the next action in the pipeline. Eventually this delegate represents the handler. + /// Cancellation token /// Awaitable task returning the - IAsyncEnumerable Handle(TRequest request, CancellationToken cancellationToken, StreamHandlerDelegate next); + IAsyncEnumerable Handle(TRequest request, StreamHandlerDelegate next, CancellationToken cancellationToken); } \ No newline at end of file diff --git a/src/MediatR/Mediator.cs b/src/MediatR/Mediator.cs index b1b6fe84..c6a639b6 100644 --- a/src/MediatR/Mediator.cs +++ b/src/MediatR/Mediator.cs @@ -38,7 +38,7 @@ public Task Send(IRequest request, Cancellation static t => (RequestHandlerBase)(Activator.CreateInstance(typeof(RequestHandlerWrapperImpl<,>).MakeGenericType(t, typeof(TResponse))) ?? throw new InvalidOperationException($"Could not create wrapper type for {t}"))); - return handler.Handle(request, cancellationToken, _serviceFactory); + return handler.Handle(request, _serviceFactory, cancellationToken); } public Task Send(object request, CancellationToken cancellationToken = default) @@ -68,7 +68,7 @@ public Task Send(IRequest request, Cancellation }); // call via dynamic dispatch to avoid calling through reflection for performance reasons - return handler.Handle(request, cancellationToken, _serviceFactory); + return handler.Handle(request, _serviceFactory, cancellationToken); } public Task Publish(TNotification notification, CancellationToken cancellationToken = default) @@ -112,7 +112,7 @@ private Task PublishNotification(INotification notification, CancellationToken c static t => (NotificationHandlerWrapper) (Activator.CreateInstance(typeof(NotificationHandlerWrapperImpl<>).MakeGenericType(t)) ?? throw new InvalidOperationException($"Could not create wrapper for type {t}"))); - return handler.Handle(notification, cancellationToken, _serviceFactory, PublishCore); + return handler.Handle(notification, _serviceFactory, PublishCore, cancellationToken); } @@ -128,7 +128,7 @@ public IAsyncEnumerable CreateStream(IStreamRequest) _streamRequestHandlers.GetOrAdd(requestType, t => (StreamRequestHandlerBase) Activator.CreateInstance(typeof(StreamRequestHandlerWrapperImpl<,>).MakeGenericType(requestType, typeof(TResponse)))); - var items = streamHandler.Handle(request, cancellationToken, _serviceFactory); + var items = streamHandler.Handle(request, _serviceFactory, cancellationToken); return items; } @@ -161,7 +161,7 @@ public IAsyncEnumerable CreateStream(IStreamRequest : IPip public RequestExceptionActionProcessorBehavior(ServiceFactory serviceFactory) => _serviceFactory = serviceFactory; - public async Task Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate next) + public async Task Handle(TRequest request, RequestHandlerDelegate next, CancellationToken cancellationToken) { try { diff --git a/src/MediatR/Pipeline/RequestExceptionProcessorBehavior.cs b/src/MediatR/Pipeline/RequestExceptionProcessorBehavior.cs index 4a2dfdbc..0bf84b25 100644 --- a/src/MediatR/Pipeline/RequestExceptionProcessorBehavior.cs +++ b/src/MediatR/Pipeline/RequestExceptionProcessorBehavior.cs @@ -23,7 +23,7 @@ public class RequestExceptionProcessorBehavior : IPipelineB public RequestExceptionProcessorBehavior(ServiceFactory serviceFactory) => _serviceFactory = serviceFactory; - public async Task Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate next) + public async Task Handle(TRequest request, RequestHandlerDelegate next, CancellationToken cancellationToken) { try { diff --git a/src/MediatR/Pipeline/RequestPostProcessorBehavior.cs b/src/MediatR/Pipeline/RequestPostProcessorBehavior.cs index 366fb641..61f7a33d 100644 --- a/src/MediatR/Pipeline/RequestPostProcessorBehavior.cs +++ b/src/MediatR/Pipeline/RequestPostProcessorBehavior.cs @@ -17,7 +17,7 @@ public class RequestPostProcessorBehavior : IPipelineBehavi public RequestPostProcessorBehavior(IEnumerable> postProcessors) => _postProcessors = postProcessors; - public async Task Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate next) + public async Task Handle(TRequest request, RequestHandlerDelegate next, CancellationToken cancellationToken) { var response = await next().ConfigureAwait(false); diff --git a/src/MediatR/Pipeline/RequestPreProcessorBehavior.cs b/src/MediatR/Pipeline/RequestPreProcessorBehavior.cs index 41820872..a3d4a913 100644 --- a/src/MediatR/Pipeline/RequestPreProcessorBehavior.cs +++ b/src/MediatR/Pipeline/RequestPreProcessorBehavior.cs @@ -17,7 +17,7 @@ public class RequestPreProcessorBehavior : IPipelineBehavio public RequestPreProcessorBehavior(IEnumerable> preProcessors) => _preProcessors = preProcessors; - public async Task Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate next) + public async Task Handle(TRequest request, RequestHandlerDelegate next, CancellationToken cancellationToken) { foreach (var processor in _preProcessors) { diff --git a/src/MediatR/Wrappers/NotificationHandlerWrapper.cs b/src/MediatR/Wrappers/NotificationHandlerWrapper.cs index bd04e6d2..0a1a970b 100644 --- a/src/MediatR/Wrappers/NotificationHandlerWrapper.cs +++ b/src/MediatR/Wrappers/NotificationHandlerWrapper.cs @@ -8,15 +8,17 @@ namespace MediatR.Wrappers; public abstract class NotificationHandlerWrapper { - public abstract Task Handle(INotification notification, CancellationToken cancellationToken, ServiceFactory serviceFactory, - Func>, INotification, CancellationToken, Task> publish); + public abstract Task Handle(INotification notification, ServiceFactory serviceFactory, + Func>, INotification, CancellationToken, Task> publish, + CancellationToken cancellationToken); } public class NotificationHandlerWrapperImpl : NotificationHandlerWrapper where TNotification : INotification { - public override Task Handle(INotification notification, CancellationToken cancellationToken, ServiceFactory serviceFactory, - Func>, INotification, CancellationToken, Task> publish) + public override Task Handle(INotification notification, ServiceFactory serviceFactory, + Func>, INotification, CancellationToken, Task> publish, + CancellationToken cancellationToken) { var handlers = serviceFactory .GetInstances>() diff --git a/src/MediatR/Wrappers/RequestHandlerWrapper.cs b/src/MediatR/Wrappers/RequestHandlerWrapper.cs index 43bab77d..ea64d039 100644 --- a/src/MediatR/Wrappers/RequestHandlerWrapper.cs +++ b/src/MediatR/Wrappers/RequestHandlerWrapper.cs @@ -6,32 +6,32 @@ namespace MediatR.Wrappers; public abstract class RequestHandlerBase : HandlerBase { - public abstract Task Handle(object request, CancellationToken cancellationToken, - ServiceFactory serviceFactory); + public abstract Task Handle(object request, ServiceFactory serviceFactory, + CancellationToken cancellationToken); } public abstract class RequestHandlerWrapper : RequestHandlerBase { - public abstract Task Handle(IRequest request, CancellationToken cancellationToken, - ServiceFactory serviceFactory); + public abstract Task Handle(IRequest request, ServiceFactory serviceFactory, + CancellationToken cancellationToken); } public class RequestHandlerWrapperImpl : RequestHandlerWrapper where TRequest : IRequest { - public override async Task Handle(object request, CancellationToken cancellationToken, - ServiceFactory serviceFactory) => - await Handle((IRequest)request, cancellationToken, serviceFactory).ConfigureAwait(false); + public override async Task Handle(object request, ServiceFactory serviceFactory, + CancellationToken cancellationToken) => + await Handle((IRequest)request, serviceFactory, cancellationToken).ConfigureAwait(false); - public override Task Handle(IRequest request, CancellationToken cancellationToken, - ServiceFactory serviceFactory) + public override Task Handle(IRequest request, ServiceFactory serviceFactory, + CancellationToken cancellationToken) { Task Handler() => GetHandler>(serviceFactory).Handle((TRequest) request, cancellationToken); return serviceFactory .GetInstances>() .Reverse() - .Aggregate((RequestHandlerDelegate) Handler, (next, pipeline) => () => pipeline.Handle((TRequest)request, cancellationToken, next))(); + .Aggregate((RequestHandlerDelegate) Handler, (next, pipeline) => () => pipeline.Handle((TRequest)request, next, cancellationToken))(); } } \ No newline at end of file diff --git a/src/MediatR/Wrappers/StreamRequestHandlerWrapper.cs b/src/MediatR/Wrappers/StreamRequestHandlerWrapper.cs index cd39e90d..d383da53 100644 --- a/src/MediatR/Wrappers/StreamRequestHandlerWrapper.cs +++ b/src/MediatR/Wrappers/StreamRequestHandlerWrapper.cs @@ -8,27 +8,27 @@ namespace MediatR.Wrappers; internal abstract class StreamRequestHandlerBase : HandlerBase { - public abstract IAsyncEnumerable Handle(object request, CancellationToken cancellationToken, ServiceFactory serviceFactory); + public abstract IAsyncEnumerable Handle(object request, ServiceFactory serviceFactory, CancellationToken cancellationToken); } internal abstract class StreamRequestHandlerWrapper : StreamRequestHandlerBase { - public abstract IAsyncEnumerable Handle(IStreamRequest request, CancellationToken cancellationToken, - ServiceFactory serviceFactory); + public abstract IAsyncEnumerable Handle(IStreamRequest request, ServiceFactory serviceFactory, + CancellationToken cancellationToken); } internal class StreamRequestHandlerWrapperImpl : StreamRequestHandlerWrapper where TRequest : IStreamRequest { - public override async IAsyncEnumerable Handle(object request, [EnumeratorCancellation] CancellationToken cancellationToken, ServiceFactory serviceFactory) + public override async IAsyncEnumerable Handle(object request, ServiceFactory serviceFactory, [EnumeratorCancellation] CancellationToken cancellationToken) { - await foreach (var item in Handle((IStreamRequest) request, cancellationToken, serviceFactory)) + await foreach (var item in Handle((IStreamRequest) request, serviceFactory, cancellationToken)) { yield return item; } } - public override async IAsyncEnumerable Handle(IStreamRequest request, [EnumeratorCancellation] CancellationToken cancellationToken, ServiceFactory serviceFactory) + public override async IAsyncEnumerable Handle(IStreamRequest request, ServiceFactory serviceFactory, [EnumeratorCancellation] CancellationToken cancellationToken) { IAsyncEnumerable Handler() => GetHandler>(serviceFactory).Handle((TRequest) request, cancellationToken); @@ -39,8 +39,8 @@ public override async IAsyncEnumerable Handle(IStreamRequest) Handler, (next, pipeline) => () => pipeline.Handle( (TRequest) request, - cancellationToken, - () => NextWrapper(next(), cancellationToken) + () => NextWrapper(next(), cancellationToken), + cancellationToken ) )(); diff --git a/test/MediatR.Tests/Pipeline/Streams/StreamPipelineBehaviorTests.cs b/test/MediatR.Tests/Pipeline/Streams/StreamPipelineBehaviorTests.cs index 01fcc115..c336b3a4 100644 --- a/test/MediatR.Tests/Pipeline/Streams/StreamPipelineBehaviorTests.cs +++ b/test/MediatR.Tests/Pipeline/Streams/StreamPipelineBehaviorTests.cs @@ -33,7 +33,7 @@ public async IAsyncEnumerable Handle(Sing request, [EnumeratorCancellation public class SingSongPipelineBehavior : IStreamPipelineBehavior { - public async IAsyncEnumerable Handle(Sing request, [EnumeratorCancellation] CancellationToken cancellationToken, StreamHandlerDelegate next) + public async IAsyncEnumerable Handle(Sing request, StreamHandlerDelegate next, [EnumeratorCancellation] CancellationToken cancellationToken) { yield return new Song { Message = "Start behaving..." }; diff --git a/test/MediatR.Tests/PipelineTests.cs b/test/MediatR.Tests/PipelineTests.cs index fc33da13..c20c3b85 100644 --- a/test/MediatR.Tests/PipelineTests.cs +++ b/test/MediatR.Tests/PipelineTests.cs @@ -69,7 +69,7 @@ public OuterBehavior(Logger output) _output = output; } - public async Task Handle(Ping request, CancellationToken cancellationToken, RequestHandlerDelegate next) + public async Task Handle(Ping request, RequestHandlerDelegate next, CancellationToken cancellationToken) { _output.Messages.Add("Outer before"); var response = await next(); @@ -88,7 +88,7 @@ public InnerBehavior(Logger output) _output = output; } - public async Task Handle(Ping request, CancellationToken cancellationToken, RequestHandlerDelegate next) + public async Task Handle(Ping request, RequestHandlerDelegate next, CancellationToken cancellationToken) { _output.Messages.Add("Inner before"); var response = await next(); @@ -108,7 +108,7 @@ public InnerBehavior(Logger output) _output = output; } - public async Task Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate next) + public async Task Handle(TRequest request, RequestHandlerDelegate next, CancellationToken cancellationToken) { _output.Messages.Add("Inner generic before"); var response = await next(); @@ -128,7 +128,7 @@ public OuterBehavior(Logger output) _output = output; } - public async Task Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate next) + public async Task Handle(TRequest request, RequestHandlerDelegate next, CancellationToken cancellationToken) { _output.Messages.Add("Outer generic before"); var response = await next(); @@ -149,7 +149,7 @@ public ConstrainedBehavior(Logger output) _output = output; } - public async Task Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate next) + public async Task Handle(TRequest request, RequestHandlerDelegate next, CancellationToken cancellationToken) { _output.Messages.Add("Constrained before"); var response = await next(); @@ -168,7 +168,7 @@ public ConcreteBehavior(Logger output) _output = output; } - public async Task Handle(Ping request, CancellationToken cancellationToken, RequestHandlerDelegate next) + public async Task Handle(Ping request, RequestHandlerDelegate next, CancellationToken cancellationToken) { _output.Messages.Add("Concrete before"); var response = await next(); diff --git a/test/MediatR.Tests/StreamPipelineTests.cs b/test/MediatR.Tests/StreamPipelineTests.cs index d3e44364..46f2ecb5 100644 --- a/test/MediatR.Tests/StreamPipelineTests.cs +++ b/test/MediatR.Tests/StreamPipelineTests.cs @@ -72,7 +72,7 @@ public OuterBehavior(Logger output) _output = output; } - public async IAsyncEnumerable Handle(Ping request, [EnumeratorCancellation] CancellationToken cancellationToken, StreamHandlerDelegate next) + public async IAsyncEnumerable Handle(Ping request, StreamHandlerDelegate next, [EnumeratorCancellation] CancellationToken cancellationToken) { _output.Messages.Add("Outer before"); await foreach (var result in next()) @@ -92,7 +92,7 @@ public InnerBehavior(Logger output) _output = output; } - public async IAsyncEnumerable Handle(Ping request, [EnumeratorCancellation] CancellationToken cancellationToken, StreamHandlerDelegate next) + public async IAsyncEnumerable Handle(Ping request, StreamHandlerDelegate next, [EnumeratorCancellation] CancellationToken cancellationToken) { _output.Messages.Add("Inner before"); await foreach (var result in next()) @@ -113,7 +113,7 @@ public InnerBehavior(Logger output) _output = output; } - public async IAsyncEnumerable Handle(TRequest request, [EnumeratorCancellation] CancellationToken cancellationToken, StreamHandlerDelegate next) + public async IAsyncEnumerable Handle(TRequest request, StreamHandlerDelegate next, [EnumeratorCancellation] CancellationToken cancellationToken) { _output.Messages.Add("Inner generic before"); await foreach (var result in next()) @@ -134,7 +134,7 @@ public OuterBehavior(Logger output) _output = output; } - public async IAsyncEnumerable Handle(TRequest request, [EnumeratorCancellation] CancellationToken cancellationToken, StreamHandlerDelegate next) + public async IAsyncEnumerable Handle(TRequest request, StreamHandlerDelegate next, [EnumeratorCancellation] CancellationToken cancellationToken) { _output.Messages.Add("Outer generic before"); await foreach (var result in next()) @@ -155,7 +155,7 @@ public ConstrainedBehavior(Logger output) { _output = output; } - public async IAsyncEnumerable Handle(TRequest request, [EnumeratorCancellation] CancellationToken cancellationToken, StreamHandlerDelegate next) + public async IAsyncEnumerable Handle(TRequest request, StreamHandlerDelegate next, [EnumeratorCancellation] CancellationToken cancellationToken) { _output.Messages.Add("Constrained before"); await foreach (var result in next()) @@ -175,7 +175,7 @@ public ConcreteBehavior(Logger output) _output = output; } - public async IAsyncEnumerable Handle(Ping request, [EnumeratorCancellation] CancellationToken cancellationToken, StreamHandlerDelegate next) + public async IAsyncEnumerable Handle(Ping request, StreamHandlerDelegate next, [EnumeratorCancellation] CancellationToken cancellationToken) { _output.Messages.Add("Concrete before"); await foreach (var result in next())