Skip to content

Commit

Permalink
Merge pull request #772 from panosru/CA1068
Browse files Browse the repository at this point in the history
CA1068: CancellationToken parameters must come last
  • Loading branch information
jbogard authored Sep 30, 2022
2 parents cd208c0 + aa3abfc commit 89c7b6d
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 48 deletions.
4 changes: 2 additions & 2 deletions src/MediatR/IPipelineBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public interface IPipelineBehavior<in TRequest, TResponse> where TRequest : IReq
/// Pipeline handler. Perform any additional behavior and await the <paramref name="next"/> delegate as necessary
/// </summary>
/// <param name="request">Incoming request</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <param name="next">Awaitable delegate for the next action in the pipeline. Eventually this delegate represents the handler.</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Awaitable task returning the <typeparamref name="TResponse"/></returns>
Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next);
Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken);
}
4 changes: 2 additions & 2 deletions src/MediatR/IStreamPipelineBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public interface IStreamPipelineBehavior<in TRequest, TResponse>
/// Stream Pipeline handler. Perform any additional behavior and iterate the <paramref name="next"/> delegate as necessary
/// </summary>
/// <param name="request">Incoming request</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <param name="next">Awaitable delegate for the next action in the pipeline. Eventually this delegate represents the handler.</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Awaitable task returning the <typeparamref name="TResponse"/></returns>
IAsyncEnumerable<TResponse> Handle(TRequest request, CancellationToken cancellationToken, StreamHandlerDelegate<TResponse> next);
IAsyncEnumerable<TResponse> Handle(TRequest request, StreamHandlerDelegate<TResponse> next, CancellationToken cancellationToken);
}
10 changes: 5 additions & 5 deletions src/MediatR/Mediator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public Task<TResponse> Send<TResponse>(IRequest<TResponse> 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<object?> Send(object request, CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -68,7 +68,7 @@ public Task<TResponse> Send<TResponse>(IRequest<TResponse> 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>(TNotification notification, CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -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);
}


Expand All @@ -128,7 +128,7 @@ public IAsyncEnumerable<TResponse> CreateStream<TResponse>(IStreamRequest<TRespo
var streamHandler = (StreamRequestHandlerWrapper<TResponse>) _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;
}
Expand Down Expand Up @@ -161,7 +161,7 @@ public IAsyncEnumerable<TResponse> CreateStream<TResponse>(IStreamRequest<TRespo
});

// call via dynamic dispatch to avoid calling through reflection for performance reasons
var items = handler.Handle(request, cancellationToken, _serviceFactory);
var items = handler.Handle(request, _serviceFactory, cancellationToken);

return items;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class RequestExceptionActionProcessorBehavior<TRequest, TResponse> : IPip

public RequestExceptionActionProcessorBehavior(ServiceFactory serviceFactory) => _serviceFactory = serviceFactory;

public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
try
{
Expand Down
2 changes: 1 addition & 1 deletion src/MediatR/Pipeline/RequestExceptionProcessorBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class RequestExceptionProcessorBehavior<TRequest, TResponse> : IPipelineB

public RequestExceptionProcessorBehavior(ServiceFactory serviceFactory) => _serviceFactory = serviceFactory;

public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
try
{
Expand Down
2 changes: 1 addition & 1 deletion src/MediatR/Pipeline/RequestPostProcessorBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class RequestPostProcessorBehavior<TRequest, TResponse> : IPipelineBehavi
public RequestPostProcessorBehavior(IEnumerable<IRequestPostProcessor<TRequest, TResponse>> postProcessors)
=> _postProcessors = postProcessors;

public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
var response = await next().ConfigureAwait(false);

Expand Down
2 changes: 1 addition & 1 deletion src/MediatR/Pipeline/RequestPreProcessorBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class RequestPreProcessorBehavior<TRequest, TResponse> : IPipelineBehavio
public RequestPreProcessorBehavior(IEnumerable<IRequestPreProcessor<TRequest>> preProcessors)
=> _preProcessors = preProcessors;

public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
foreach (var processor in _preProcessors)
{
Expand Down
10 changes: 6 additions & 4 deletions src/MediatR/Wrappers/NotificationHandlerWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ namespace MediatR.Wrappers;

public abstract class NotificationHandlerWrapper
{
public abstract Task Handle(INotification notification, CancellationToken cancellationToken, ServiceFactory serviceFactory,
Func<IEnumerable<Func<INotification, CancellationToken, Task>>, INotification, CancellationToken, Task> publish);
public abstract Task Handle(INotification notification, ServiceFactory serviceFactory,
Func<IEnumerable<Func<INotification, CancellationToken, Task>>, INotification, CancellationToken, Task> publish,
CancellationToken cancellationToken);
}

public class NotificationHandlerWrapperImpl<TNotification> : NotificationHandlerWrapper
where TNotification : INotification
{
public override Task Handle(INotification notification, CancellationToken cancellationToken, ServiceFactory serviceFactory,
Func<IEnumerable<Func<INotification, CancellationToken, Task>>, INotification, CancellationToken, Task> publish)
public override Task Handle(INotification notification, ServiceFactory serviceFactory,
Func<IEnumerable<Func<INotification, CancellationToken, Task>>, INotification, CancellationToken, Task> publish,
CancellationToken cancellationToken)
{
var handlers = serviceFactory
.GetInstances<INotificationHandler<TNotification>>()
Expand Down
20 changes: 10 additions & 10 deletions src/MediatR/Wrappers/RequestHandlerWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@ namespace MediatR.Wrappers;

public abstract class RequestHandlerBase : HandlerBase
{
public abstract Task<object?> Handle(object request, CancellationToken cancellationToken,
ServiceFactory serviceFactory);
public abstract Task<object?> Handle(object request, ServiceFactory serviceFactory,
CancellationToken cancellationToken);

}

public abstract class RequestHandlerWrapper<TResponse> : RequestHandlerBase
{
public abstract Task<TResponse> Handle(IRequest<TResponse> request, CancellationToken cancellationToken,
ServiceFactory serviceFactory);
public abstract Task<TResponse> Handle(IRequest<TResponse> request, ServiceFactory serviceFactory,
CancellationToken cancellationToken);
}

public class RequestHandlerWrapperImpl<TRequest, TResponse> : RequestHandlerWrapper<TResponse>
where TRequest : IRequest<TResponse>
{
public override async Task<object?> Handle(object request, CancellationToken cancellationToken,
ServiceFactory serviceFactory) =>
await Handle((IRequest<TResponse>)request, cancellationToken, serviceFactory).ConfigureAwait(false);
public override async Task<object?> Handle(object request, ServiceFactory serviceFactory,
CancellationToken cancellationToken) =>
await Handle((IRequest<TResponse>)request, serviceFactory, cancellationToken).ConfigureAwait(false);

public override Task<TResponse> Handle(IRequest<TResponse> request, CancellationToken cancellationToken,
ServiceFactory serviceFactory)
public override Task<TResponse> Handle(IRequest<TResponse> request, ServiceFactory serviceFactory,
CancellationToken cancellationToken)
{
Task<TResponse> Handler() => GetHandler<IRequestHandler<TRequest, TResponse>>(serviceFactory).Handle((TRequest) request, cancellationToken);

return serviceFactory
.GetInstances<IPipelineBehavior<TRequest, TResponse>>()
.Reverse()
.Aggregate((RequestHandlerDelegate<TResponse>) Handler, (next, pipeline) => () => pipeline.Handle((TRequest)request, cancellationToken, next))();
.Aggregate((RequestHandlerDelegate<TResponse>) Handler, (next, pipeline) => () => pipeline.Handle((TRequest)request, next, cancellationToken))();
}
}
16 changes: 8 additions & 8 deletions src/MediatR/Wrappers/StreamRequestHandlerWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@ namespace MediatR.Wrappers;

internal abstract class StreamRequestHandlerBase : HandlerBase
{
public abstract IAsyncEnumerable<object?> Handle(object request, CancellationToken cancellationToken, ServiceFactory serviceFactory);
public abstract IAsyncEnumerable<object?> Handle(object request, ServiceFactory serviceFactory, CancellationToken cancellationToken);
}

internal abstract class StreamRequestHandlerWrapper<TResponse> : StreamRequestHandlerBase
{
public abstract IAsyncEnumerable<TResponse> Handle(IStreamRequest<TResponse> request, CancellationToken cancellationToken,
ServiceFactory serviceFactory);
public abstract IAsyncEnumerable<TResponse> Handle(IStreamRequest<TResponse> request, ServiceFactory serviceFactory,
CancellationToken cancellationToken);
}

internal class StreamRequestHandlerWrapperImpl<TRequest, TResponse> : StreamRequestHandlerWrapper<TResponse>
where TRequest : IStreamRequest<TResponse>
{
public override async IAsyncEnumerable<object?> Handle(object request, [EnumeratorCancellation] CancellationToken cancellationToken, ServiceFactory serviceFactory)
public override async IAsyncEnumerable<object?> Handle(object request, ServiceFactory serviceFactory, [EnumeratorCancellation] CancellationToken cancellationToken)
{
await foreach (var item in Handle((IStreamRequest<TResponse>) request, cancellationToken, serviceFactory))
await foreach (var item in Handle((IStreamRequest<TResponse>) request, serviceFactory, cancellationToken))
{
yield return item;
}
}

public override async IAsyncEnumerable<TResponse> Handle(IStreamRequest<TResponse> request, [EnumeratorCancellation] CancellationToken cancellationToken, ServiceFactory serviceFactory)
public override async IAsyncEnumerable<TResponse> Handle(IStreamRequest<TResponse> request, ServiceFactory serviceFactory, [EnumeratorCancellation] CancellationToken cancellationToken)
{
IAsyncEnumerable<TResponse> Handler() => GetHandler<IStreamRequestHandler<TRequest, TResponse>>(serviceFactory).Handle((TRequest) request, cancellationToken);

Expand All @@ -39,8 +39,8 @@ public override async IAsyncEnumerable<TResponse> Handle(IStreamRequest<TRespons
(StreamHandlerDelegate<TResponse>) Handler,
(next, pipeline) => () => pipeline.Handle(
(TRequest) request,
cancellationToken,
() => NextWrapper(next(), cancellationToken)
() => NextWrapper(next(), cancellationToken),
cancellationToken
)
)();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async IAsyncEnumerable<Song> Handle(Sing request, [EnumeratorCancellation

public class SingSongPipelineBehavior : IStreamPipelineBehavior<Sing, Song>
{
public async IAsyncEnumerable<Song> Handle(Sing request, [EnumeratorCancellation] CancellationToken cancellationToken, StreamHandlerDelegate<Song> next)
public async IAsyncEnumerable<Song> Handle(Sing request, StreamHandlerDelegate<Song> next, [EnumeratorCancellation] CancellationToken cancellationToken)
{
yield return new Song { Message = "Start behaving..." };

Expand Down
12 changes: 6 additions & 6 deletions test/MediatR.Tests/PipelineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public OuterBehavior(Logger output)
_output = output;
}

public async Task<Pong> Handle(Ping request, CancellationToken cancellationToken, RequestHandlerDelegate<Pong> next)
public async Task<Pong> Handle(Ping request, RequestHandlerDelegate<Pong> next, CancellationToken cancellationToken)
{
_output.Messages.Add("Outer before");
var response = await next();
Expand All @@ -88,7 +88,7 @@ public InnerBehavior(Logger output)
_output = output;
}

public async Task<Pong> Handle(Ping request, CancellationToken cancellationToken, RequestHandlerDelegate<Pong> next)
public async Task<Pong> Handle(Ping request, RequestHandlerDelegate<Pong> next, CancellationToken cancellationToken)
{
_output.Messages.Add("Inner before");
var response = await next();
Expand All @@ -108,7 +108,7 @@ public InnerBehavior(Logger output)
_output = output;
}

public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
_output.Messages.Add("Inner generic before");
var response = await next();
Expand All @@ -128,7 +128,7 @@ public OuterBehavior(Logger output)
_output = output;
}

public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
_output.Messages.Add("Outer generic before");
var response = await next();
Expand All @@ -149,7 +149,7 @@ public ConstrainedBehavior(Logger output)
_output = output;
}

public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TResponse> next, CancellationToken cancellationToken)
{
_output.Messages.Add("Constrained before");
var response = await next();
Expand All @@ -168,7 +168,7 @@ public ConcreteBehavior(Logger output)
_output = output;
}

public async Task<Pong> Handle(Ping request, CancellationToken cancellationToken, RequestHandlerDelegate<Pong> next)
public async Task<Pong> Handle(Ping request, RequestHandlerDelegate<Pong> next, CancellationToken cancellationToken)
{
_output.Messages.Add("Concrete before");
var response = await next();
Expand Down
12 changes: 6 additions & 6 deletions test/MediatR.Tests/StreamPipelineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public OuterBehavior(Logger output)
_output = output;
}

public async IAsyncEnumerable<Pong> Handle(Ping request, [EnumeratorCancellation] CancellationToken cancellationToken, StreamHandlerDelegate<Pong> next)
public async IAsyncEnumerable<Pong> Handle(Ping request, StreamHandlerDelegate<Pong> next, [EnumeratorCancellation] CancellationToken cancellationToken)
{
_output.Messages.Add("Outer before");
await foreach (var result in next())
Expand All @@ -92,7 +92,7 @@ public InnerBehavior(Logger output)
_output = output;
}

public async IAsyncEnumerable<Pong> Handle(Ping request, [EnumeratorCancellation] CancellationToken cancellationToken, StreamHandlerDelegate<Pong> next)
public async IAsyncEnumerable<Pong> Handle(Ping request, StreamHandlerDelegate<Pong> next, [EnumeratorCancellation] CancellationToken cancellationToken)
{
_output.Messages.Add("Inner before");
await foreach (var result in next())
Expand All @@ -113,7 +113,7 @@ public InnerBehavior(Logger output)
_output = output;
}

public async IAsyncEnumerable<TResponse> Handle(TRequest request, [EnumeratorCancellation] CancellationToken cancellationToken, StreamHandlerDelegate<TResponse> next)
public async IAsyncEnumerable<TResponse> Handle(TRequest request, StreamHandlerDelegate<TResponse> next, [EnumeratorCancellation] CancellationToken cancellationToken)
{
_output.Messages.Add("Inner generic before");
await foreach (var result in next())
Expand All @@ -134,7 +134,7 @@ public OuterBehavior(Logger output)
_output = output;
}

public async IAsyncEnumerable<TResponse> Handle(TRequest request, [EnumeratorCancellation] CancellationToken cancellationToken, StreamHandlerDelegate<TResponse> next)
public async IAsyncEnumerable<TResponse> Handle(TRequest request, StreamHandlerDelegate<TResponse> next, [EnumeratorCancellation] CancellationToken cancellationToken)
{
_output.Messages.Add("Outer generic before");
await foreach (var result in next())
Expand All @@ -155,7 +155,7 @@ public ConstrainedBehavior(Logger output)
{
_output = output;
}
public async IAsyncEnumerable<TResponse> Handle(TRequest request, [EnumeratorCancellation] CancellationToken cancellationToken, StreamHandlerDelegate<TResponse> next)
public async IAsyncEnumerable<TResponse> Handle(TRequest request, StreamHandlerDelegate<TResponse> next, [EnumeratorCancellation] CancellationToken cancellationToken)
{
_output.Messages.Add("Constrained before");
await foreach (var result in next())
Expand All @@ -175,7 +175,7 @@ public ConcreteBehavior(Logger output)
_output = output;
}

public async IAsyncEnumerable<Pong> Handle(Ping request, [EnumeratorCancellation] CancellationToken cancellationToken, StreamHandlerDelegate<Pong> next)
public async IAsyncEnumerable<Pong> Handle(Ping request, StreamHandlerDelegate<Pong> next, [EnumeratorCancellation] CancellationToken cancellationToken)
{
_output.Messages.Add("Concrete before");
await foreach (var result in next())
Expand Down

0 comments on commit 89c7b6d

Please sign in to comment.