I'm new to MediatR, trying to make request validation using pipeline behavior, all the examples that I came across were throwing ValidationException if any errors happening.
below code is an example of validation pipeline:
public class ValidationPipeline<TRequest, TResponse> : IPipelineBehavior<TRequest, TResponse>
where TRequest : IRequest<TResponse>
{
private readonly IEnumerable<IValidator> _validators;
public ValidationPipeline(IEnumerable<IValidator<TRequest>> validators)
{
_validators = validators;
}
public Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
{
var context = new ValidationContext<TRequest>(request);
var validationFailures = _validators
.Select(validator => validator.Validate(context))
.SelectMany(validationResult => validationResult.Errors)
.Where(validationFailure => validationFailure != null)
.ToList();
if (validationFailures.Any())
{
throw new FluentValidation.ValidationException(validationFailures);
}
return next();
}
}
this method works fine, but I want to return the response with validation errors (without) throwing exception, so I tried this:
public class ValidationBehavior<TRequest, TResponse> : IPipelineBehavior<TRequest, BaseResponse<TResponse>>
where TRequest : IRequest<BaseResponse<TResponse>>
{
private readonly IEnumerable<IValidator> _validators;
public ValidationBehavior(IEnumerable<IValidator<TRequest>> validators)
{
_validators = validators;
}
public Task<BaseResponse<TResponse>> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<BaseResponse<TResponse>> next)
{
var context = new ValidationContext<TRequest>(request);
var validationFailures = _validators
.Select(validator => validator.Validate(context))
.SelectMany(validationResult => validationResult.Errors)
.Where(validationFailure => validationFailure != null)
.ToList();
if (validationFailures.Any())
{
return Task.FromResult(new BaseResponse<TResponse>
{
Code = 400,
Message = "Validation error",
Error = validationFailures.Select(err => err.ErrorMessage)
});
}
else
{
return next();
}
}
but now the validation pipeline code does not execute,
and execution go to regular handlers (ex: Register User Handler).
my response (used in all handlers):
public class BaseResponse<TResponse>
{
public int Code { get; set; }
public string Message { get; set; }
public TResponse Result { get; set; }
public object Error { get; set; }
public string TraceIdentifier { get; set; }
}
register the behaviour with DI like this:
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
any help will be appreciated.
BaseResponse<TResponse>
causes the entireHandle
method to never execute? That doesn't sound right, obviously. You're sure you're not swallowing/ignoring some unhandled exception that may be happening there? – LalitaIRequest<BaseResponse<TResponse>>
? If not, then this pipeline behaviour will not execute. – Astaire