Код: Выделить всё
public class ValidationBehavior : IPipelineBehavior where T1 : IRequest where T2 : Outcome, new() {
private readonly IEnumerable _validators;
public ValidationBehavior(IEnumerable validators) {
_validators = validators;
} // ValidationBehavior
public Task Handle(T1 request, RequestHandlerDelegate next, CancellationToken cancellationToken) {
List errors = _validators
.Select(x => x.Validate(request))
.SelectMany(x => x.Errors)
.Where(x => x != null)
.Select(x => new Error(x.PropertyName, x.ErrorMessage))
.ToList();
if (errors.Any()) {
T2 Outcome = new T2 { Status = Status.Invalid, Payload = new Payload(errors) };
return Task.FromResult(Outcome);
}
return next();
} // Handle
Код: Выделить всё
builder.Services.AddMediatR(x => {
x.RegisterServicesFromAssemblyContaining
();
x.AddOpenBehavior(typeof(ValidationBehavior));
}).AddBehaviors();
Код: Выделить всё
System.ArgumentException: Arity of open generic service type 'MediatR.IPipelineBehavior`2[TRequest,TResponse]' does not equal arity of open generic implementation type 'ValidationBehavior`3[TRequest,TResponse,T]'. (Parameter 'descriptors')
Код: Выделить всё
services.AddMediatR(cfg => {
cfg.AddBehavior();
cfg.AddOpenBehavior(typeof(OuterBehavior));
cfg.AddOpenBehavior(typeof(InnerBehavior));
cfg.AddOpenBehavior(typeof(ConstrainedBehavior));
});
Подробнее здесь: https://stackoverflow.com/questions/791 ... eneric-imp