Exemple #1
0
        public async Task <TResult> DispatchAsync <TResult>(IQueryAsync <TResult> query)
        {
            using var scope = _provider.CreateScope();
            var type        = typeof(IQueryHandlerAsync <,>);
            var handlerType = type.MakeGenericType(query.GetType(), typeof(TResult));
            var handler     = scope.ServiceProvider.GetRequiredService(handlerType);
            var handle      = handlerType.GetMethod("HandleAsync");

            if (handle == null)
            {
                throw new InvalidOperationException(
                          $"Method 'Handle' not found on type {handlerType.FullName}");
            }

            var result = await(Task <TResult>) handle.Invoke(handler, new object[] { query });

            return(result);
        }