Example #1
0
        public async Task <TResponse> Handle(TRequest request, CancellationToken cancellationToken,
                                             MediatR.RequestHandlerDelegate <TResponse> next)
        {
            var       name = typeof(TRequest).Name;
            TResponse response;

            _logger.LogInformation("{Name} handler started. [Guid:{RequestGuid}][CorrelationId:{CorrelationId}]",
                                   name, request.RequestGuid, request.CorrelationId);
            try
            {
                response = await next();
            }
            catch (CommandValidationException)
            {
                _logger.LogWarning("{Name} handler validation failed. [{RequestGuid}][CorrelationId:{CorrelationId}]",
                                   name, request.RequestGuid, request.CorrelationId);
                throw;
            }
            catch
            {
                _logger.LogError("{Name} handler failed with exception. [{RequestGuid}][CorrelationId:{CorrelationId}]",
                                 name, request.RequestGuid, request.CorrelationId);
                throw;
            }

            _logger.LogInformation("{Name} handler finished. [{RequestGuid}][CorrelationId:{CorrelationId}]",
                                   name, request.RequestGuid, request.CorrelationId);

            return(response);
        }
Example #2
0
        Handle(TRequest request,
               System.Threading.CancellationToken cancellationToken,
               MediatR.RequestHandlerDelegate <TResponse> next)
        {
            if (Validators.Any())
            {
                var context =
                    new FluentValidation.ValidationContext <TRequest>(request);

                var validationResults =
                    await System.Threading.Tasks.Task.WhenAll
                        (Validators.Select(v => v.ValidateAsync(context, cancellationToken)));

                var failures =
                    validationResults
                    .SelectMany(current => current.Errors)
                    .Where(current => current != null)
                    .ToList()
                ;

                if (failures.Count != 0)
                {
                    throw new FluentValidation.ValidationException(errors: failures);
                }
            }

            return(await next());
        }