diff --git a/samples/MediatR.Examples/JingHandler.cs b/samples/MediatR.Examples/JingHandler.cs index bcc5af9c..bd94114f 100644 --- a/samples/MediatR.Examples/JingHandler.cs +++ b/samples/MediatR.Examples/JingHandler.cs @@ -13,7 +13,7 @@ public JingHandler(TextWriter writer) _writer = writer; } - protected override Task Handle(Jing request) + protected override Task Handle(Jing request, CancellationToken cancellationToken) { return _writer.WriteLineAsync($"--- Handled Jing: {request.Message}, no Jong"); } diff --git a/src/MediatR/INotificationHandler.cs b/src/MediatR/INotificationHandler.cs index ce4b0641..0f0f71b5 100644 --- a/src/MediatR/INotificationHandler.cs +++ b/src/MediatR/INotificationHandler.cs @@ -25,7 +25,7 @@ public interface INotificationHandler public abstract class NotificationHandler : INotificationHandler where TNotification : INotification { - public Task Handle(TNotification notification, CancellationToken cancellationToken) + Task INotificationHandler.Handle(TNotification notification, CancellationToken cancellationToken) { Handle(notification); return Unit.Task; @@ -37,22 +37,4 @@ public Task Handle(TNotification notification, CancellationToken cancellationTok /// Notification protected abstract void Handle(TNotification notification); } - - /// - /// Wrapper class for an async notification handler, ignoring the cancellation token - /// - /// The notification type - public abstract class AsyncNotificationHandler : INotificationHandler - where TNotification : INotification - { - public Task Handle(TNotification notification, CancellationToken cancellationToken) - => Handle(notification); - - /// - /// Override in a derived class for the handler logic - /// - /// Notification - /// A task - protected abstract Task Handle(TNotification notification); - } } diff --git a/src/MediatR/IRequestHandler.cs b/src/MediatR/IRequestHandler.cs index 554b5371..c0d07455 100644 --- a/src/MediatR/IRequestHandler.cs +++ b/src/MediatR/IRequestHandler.cs @@ -30,36 +30,16 @@ public interface IRequestHandler : IRequestHandler { } - - /// - /// Wrapper class for a handler that asynchronously handles a request and returns a response, ignoring the cancellation token - /// - /// The type of request being handled - /// The type of response from the handler - public abstract class AsyncRequestHandler : IRequestHandler - where TRequest : IRequest - { - public Task Handle(TRequest request, CancellationToken cancellationToken) - => Handle(request); - - /// - /// Override in a derived class for the handler logic - /// - /// Request - /// Response - protected abstract Task Handle(TRequest request); - } - /// - /// Wrapper class for a handler that asynchronously handles a request and does not return a response, ignoring the cancellation token + /// Wrapper class for a handler that asynchronously handles a request and does not return a response /// /// The type of request being handled public abstract class AsyncRequestHandler : IRequestHandler where TRequest : IRequest { - public async Task Handle(TRequest request, CancellationToken cancellationToken) + async Task IRequestHandler.Handle(TRequest request, CancellationToken cancellationToken) { - await Handle(request).ConfigureAwait(false); + await Handle(request, cancellationToken).ConfigureAwait(false); return Unit.Value; } @@ -67,8 +47,9 @@ public async Task Handle(TRequest request, CancellationToken cancellationT /// Override in a derived class for the handler logic /// /// Request + /// /// Response - protected abstract Task Handle(TRequest request); + protected abstract Task Handle(TRequest request, CancellationToken cancellationToken); } /// @@ -79,7 +60,7 @@ public async Task Handle(TRequest request, CancellationToken cancellationT public abstract class RequestHandler : IRequestHandler where TRequest : IRequest { - public Task Handle(TRequest request, CancellationToken cancellationToken) + Task IRequestHandler.Handle(TRequest request, CancellationToken cancellationToken) => Task.FromResult(Handle(request)); /// @@ -97,7 +78,7 @@ public Task Handle(TRequest request, CancellationToken cancellationTo public abstract class RequestHandler : IRequestHandler where TRequest : IRequest { - public Task Handle(TRequest request, CancellationToken cancellationToken) + Task IRequestHandler.Handle(TRequest request, CancellationToken cancellationToken) { Handle(request); return Unit.Task; diff --git a/test/MediatR.Tests/SendVoidInterfaceTests.cs b/test/MediatR.Tests/SendVoidInterfaceTests.cs index d619c029..03702f42 100644 --- a/test/MediatR.Tests/SendVoidInterfaceTests.cs +++ b/test/MediatR.Tests/SendVoidInterfaceTests.cs @@ -1,3 +1,5 @@ +using System.Threading; + namespace MediatR.Tests { using System.IO; @@ -20,7 +22,7 @@ public class PingHandler : AsyncRequestHandler public PingHandler(TextWriter writer) => _writer = writer; - protected override Task Handle(Ping request) + protected override Task Handle(Ping request, CancellationToken cancellationToken) => _writer.WriteAsync(request.Message + " Pong"); }