From 7af3626856920222d21877dfb1aac24fd3dc54a7 Mon Sep 17 00:00:00 2001 From: Allan Ritchie Date: Tue, 4 Jun 2024 00:22:49 -0400 Subject: [PATCH] Fix scope & add void middleware --- .../Impl/DefaultRequestSender.cs | 27 +++++-------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/src/Shiny.Mediator/Impl/DefaultRequestSender.cs b/src/Shiny.Mediator/Impl/DefaultRequestSender.cs index ef3bffc..3cad344 100644 --- a/src/Shiny.Mediator/Impl/DefaultRequestSender.cs +++ b/src/Shiny.Mediator/Impl/DefaultRequestSender.cs @@ -9,25 +9,12 @@ public class DefaultRequestSender(IServiceProvider services) : IRequestSender { public async Task Send(IRequest request, CancellationToken cancellationToken) { - // using var scope = services.CreateScope(); - // var handlers = scope.ServiceProvider.GetServices>().ToList(); - // AssertRequestHandlers(handlers.Count, request); - // - // await this.ExecuteMiddleware( - // scope, - // (IRequest)request, - // async () => - // { - // await handlers - // .First() - // .Handle(request, cancellationToken) - // .ConfigureAwait(false); - // return Unit.Value; - // }, - // cancellationToken - // ) - // .ConfigureAwait(false); - throw new BadImageFormatException(); + using var scope = services.CreateScope(); + var wrapperType = typeof(RequestWrapper<,>).MakeGenericType([request.GetType(), typeof(Unit)]); + var wrapperMethod = wrapperType.GetMethod("Handle", BindingFlags.Public | BindingFlags.Instance)!; + var wrapper = Activator.CreateInstance(wrapperType); + var task = (Task)wrapperMethod.Invoke(wrapper, [scope.ServiceProvider, request, cancellationToken])!; + await task.ConfigureAwait(false); } @@ -37,7 +24,7 @@ public async Task Request(IRequest request, Cancellat var wrapperType = typeof(RequestWrapper<,>).MakeGenericType([request.GetType(), typeof(TResult)]); var wrapperMethod = wrapperType.GetMethod("Handle", BindingFlags.Public | BindingFlags.Instance)!; var wrapper = Activator.CreateInstance(wrapperType); - var task = (Task)wrapperMethod.Invoke(wrapper, [services, request, cancellationToken])!; + var task = (Task)wrapperMethod.Invoke(wrapper, [scope.ServiceProvider, request, cancellationToken])!; var result = await task.ConfigureAwait(false); return result; }