public Task ProcessAsync(ICommandAsync command, CancellationToken token = default(CancellationToken)) { if (command == null) { throw new ArgumentNullException("command"); } var handlerType = typeof(ICommandHandlerAsync <>).MakeGenericType(command.GetType()); dynamic handler = _serviceProvider.GetService(handlerType); return(handler.Handle((dynamic)command, (dynamic)token)); }
public async Task <Result <TResult> > DispatchAsync <TResult>(ICommandAsync <TResult> command) where TResult : struct { using var scope = _provider.CreateScope(); var type = typeof(ICommandHandlerAsync <,>); var handlerType = type.MakeGenericType(command.GetType(), typeof(TResult)); var handler = scope.ServiceProvider.GetRequiredService(handlerType); var handle = handlerType.GetMethod("HandleAsync"); if (handle == null) { throw new InvalidOperationException( $"Method 'HandleAsync' not found on type {handlerType.FullName}"); } var result = await(Task <Result <TResult> >) handle.Invoke(handler, new object[] { command }); return(result); }