From eaaa3305ba0a14ac07fc95062164ae49a3be82d8 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 17 Sep 2020 21:05:39 +1000 Subject: [PATCH 1/6] Update Mediator.cs --- src/MediatR/Mediator.cs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/MediatR/Mediator.cs b/src/MediatR/Mediator.cs index 6dbb443b..53c9ef3f 100644 --- a/src/MediatR/Mediator.cs +++ b/src/MediatR/Mediator.cs @@ -48,19 +48,22 @@ public Task Send(IRequest request, Cancellation throw new ArgumentNullException(nameof(request)); } var requestType = request.GetType(); - var requestInterfaceType = requestType - .GetInterfaces() - .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IRequest<>)); - var isValidRequest = requestInterfaceType != null; - - if (!isValidRequest) - { - throw new ArgumentException($"{nameof(request)} does not implement ${nameof(IRequest)}"); - } - - var responseType = requestInterfaceType!.GetGenericArguments()[0]; var handler = _requestHandlers.GetOrAdd(requestType, - t => Activator.CreateInstance(typeof(RequestHandlerWrapperImpl<,>).MakeGenericType(requestType, responseType))); + t => + { + var requestInterfaceType = requestType + .GetInterfaces() + .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IRequest<>)); + var isValidRequest = requestInterfaceType != null; + + if (!isValidRequest) + { + throw new ArgumentException($"{nameof(request)} does not implement ${nameof(IRequest)}"); + } + + var responseType = requestInterfaceType!.GetGenericArguments()[0]; + return Activator.CreateInstance(typeof(RequestHandlerWrapperImpl<,>).MakeGenericType(requestType, responseType)); + }); // call via dynamic dispatch to avoid calling through reflection for performance reasons return ((RequestHandlerBase) handler).Handle(request, cancellationToken, _serviceFactory); From 331694ce992af3c0e8ab629839aefa6d00bb9577 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Fri, 18 Sep 2020 09:10:07 +1000 Subject: [PATCH 2/6] Update Mediator.cs --- src/MediatR/Mediator.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/MediatR/Mediator.cs b/src/MediatR/Mediator.cs index 4509276a..3c5f2584 100644 --- a/src/MediatR/Mediator.cs +++ b/src/MediatR/Mediator.cs @@ -49,9 +49,9 @@ public Task Send(IRequest request, Cancellation } var requestType = request.GetType(); var handler = _requestHandlers.GetOrAdd(requestType, - t => + requestTypeKey => { - var requestInterfaceType = requestType + var requestInterfaceType = requestTypeKey .GetInterfaces() .FirstOrDefault(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IRequest<>)); var isValidRequest = requestInterfaceType != null; @@ -62,7 +62,7 @@ public Task Send(IRequest request, Cancellation } var responseType = requestInterfaceType!.GetGenericArguments()[0]; - return (RequestHandlerBase)Activator.CreateInstance(typeof(RequestHandlerWrapperImpl<,>).MakeGenericType(requestType, responseType)); + return (RequestHandlerBase)Activator.CreateInstance(typeof(RequestHandlerWrapperImpl<,>).MakeGenericType(requestTypeKey, responseType)); }); // call via dynamic dispatch to avoid calling through reflection for performance reasons From fd8f855dbb86cdd92d3968a40c5a6ee2eb06b046 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Tue, 22 Sep 2020 15:33:11 +1000 Subject: [PATCH 3/6] addTest --- test/MediatR.Tests/ExceptionTests.cs | 29 +++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/test/MediatR.Tests/ExceptionTests.cs b/test/MediatR.Tests/ExceptionTests.cs index 16a1f26d..95e444d2 100644 --- a/test/MediatR.Tests/ExceptionTests.cs +++ b/test/MediatR.Tests/ExceptionTests.cs @@ -276,7 +276,34 @@ public async Task Should_throw_exception_for_non_generic_send_when_exception_occ await Should.ThrowAsync(async () => await mediator.Send(pingException)); } - + + [Fact] + public async Task Should_throw_exception_for_non_request_send() + { + var container = new Container(cfg => + { + cfg.Scan(scanner => + { + scanner.AssemblyContainingType(typeof(NullPinged)); + scanner.IncludeNamespaceContainingType(); + scanner.WithDefaultConventions(); + scanner.AddAllTypesOf(typeof(IRequestHandler<,>)); + }); + cfg.For().Use(ctx => t => ctx.GetInstance(t)); + cfg.For().Use(); + }); + var mediator = container.GetInstance(); + + object nonRequest = new NonRequest(); + + await Should.ThrowAsync(async () => await mediator.Send(nonRequest)); + } + + public class NonRequest + { + + } + [Fact] public async Task Should_throw_exception_for_generic_send_when_exception_occurs() { From 4e6c3547ad3aecc55b2e7cc95965a7cf10cb8c30 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Tue, 22 Sep 2020 16:09:50 +1000 Subject: [PATCH 4/6] fix exception message --- src/MediatR/Mediator.cs | 2 +- test/MediatR.Tests/ExceptionTests.cs | 17 +++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/MediatR/Mediator.cs b/src/MediatR/Mediator.cs index 3c5f2584..eb45d271 100644 --- a/src/MediatR/Mediator.cs +++ b/src/MediatR/Mediator.cs @@ -58,7 +58,7 @@ public Task Send(IRequest request, Cancellation if (!isValidRequest) { - throw new ArgumentException($"{nameof(request)} does not implement ${nameof(IRequest)}"); + throw new ArgumentException($"{requestType.Name} does not implement {nameof(IRequest)}"); } var responseType = requestInterfaceType!.GetGenericArguments()[0]; diff --git a/test/MediatR.Tests/ExceptionTests.cs b/test/MediatR.Tests/ExceptionTests.cs index 95e444d2..ed0d846e 100644 --- a/test/MediatR.Tests/ExceptionTests.cs +++ b/test/MediatR.Tests/ExceptionTests.cs @@ -244,9 +244,9 @@ public async Task Should_throw_argument_exception_for_publish_when_request_is_no public class PingException : IRequest { - + } - + public class PingExceptionHandler : IRequestHandler { public Task Handle(PingException request, CancellationToken cancellationToken) @@ -254,7 +254,7 @@ public Task Handle(PingException request, CancellationToken cancellationTo throw new NotImplementedException(); } } - + [Fact] public async Task Should_throw_exception_for_non_generic_send_when_exception_occurs() { @@ -271,9 +271,9 @@ public async Task Should_throw_exception_for_non_generic_send_when_exception_occ cfg.For().Use(); }); var mediator = container.GetInstance(); - + object pingException = new PingException(); - + await Should.ThrowAsync(async () => await mediator.Send(pingException)); } @@ -296,7 +296,8 @@ public async Task Should_throw_exception_for_non_request_send() object nonRequest = new NonRequest(); - await Should.ThrowAsync(async () => await mediator.Send(nonRequest)); + var argumentException = await Should.ThrowAsync(async () => await mediator.Send(nonRequest)); + Assert.Equal("NonRequest does not implement IRequest", argumentException.Message); } public class NonRequest @@ -320,9 +321,9 @@ public async Task Should_throw_exception_for_generic_send_when_exception_occurs( cfg.For().Use(); }); var mediator = container.GetInstance(); - + PingException pingException = new PingException(); - + await Should.ThrowAsync(async () => await mediator.Send(pingException)); } } From 6e20bc72d65d3855c77722fdbd850b8d590c32c2 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 24 Sep 2020 08:06:28 +1000 Subject: [PATCH 5/6] add param name --- src/MediatR/Mediator.cs | 2 +- test/MediatR.Tests/ExceptionTests.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MediatR/Mediator.cs b/src/MediatR/Mediator.cs index eb45d271..c4978e3c 100644 --- a/src/MediatR/Mediator.cs +++ b/src/MediatR/Mediator.cs @@ -58,7 +58,7 @@ public Task Send(IRequest request, Cancellation if (!isValidRequest) { - throw new ArgumentException($"{requestType.Name} does not implement {nameof(IRequest)}"); + throw new ArgumentException($"{requestType.Name} does not implement {nameof(IRequest)}", nameof(request)); } var responseType = requestInterfaceType!.GetGenericArguments()[0]; diff --git a/test/MediatR.Tests/ExceptionTests.cs b/test/MediatR.Tests/ExceptionTests.cs index ed0d846e..aa1ecbf2 100644 --- a/test/MediatR.Tests/ExceptionTests.cs +++ b/test/MediatR.Tests/ExceptionTests.cs @@ -297,7 +297,7 @@ public async Task Should_throw_exception_for_non_request_send() object nonRequest = new NonRequest(); var argumentException = await Should.ThrowAsync(async () => await mediator.Send(nonRequest)); - Assert.Equal("NonRequest does not implement IRequest", argumentException.Message); + Assert.Equal("NonRequest does not implement IRequest\r\nParameter name: request", argumentException.Message); } public class NonRequest From 678773f243d485ea310f6fcb073e9fcfe0e703f0 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Thu, 24 Sep 2020 09:06:55 +1000 Subject: [PATCH 6/6] Update ExceptionTests.cs --- test/MediatR.Tests/ExceptionTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/MediatR.Tests/ExceptionTests.cs b/test/MediatR.Tests/ExceptionTests.cs index aa1ecbf2..fbe39570 100644 --- a/test/MediatR.Tests/ExceptionTests.cs +++ b/test/MediatR.Tests/ExceptionTests.cs @@ -297,7 +297,7 @@ public async Task Should_throw_exception_for_non_request_send() object nonRequest = new NonRequest(); var argumentException = await Should.ThrowAsync(async () => await mediator.Send(nonRequest)); - Assert.Equal("NonRequest does not implement IRequest\r\nParameter name: request", argumentException.Message); + Assert.StartsWith("NonRequest does not implement IRequest", argumentException.Message); } public class NonRequest