Closed neozhu closed 1 year ago
//add RequestExceptionProcessorBehavior before ValidationBehaviour it's working
services.AddMediatR(config=> {
config.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly());
config.NotificationPublisher = new ParallelNoWaitPublisher();
config.AddOpenBehavior(typeof(RequestExceptionProcessorBehavior<,>));
config.AddOpenBehavior(typeof(ValidationBehaviour<,>));
config.AddOpenBehavior(typeof(AuthorizationBehaviour<,>));
config.AddOpenBehavior(typeof(MemoryCacheBehaviour<,>));
config.AddOpenBehavior(typeof(CacheInvalidationBehaviour<,>));
config.AddOpenBehavior(typeof(PerformanceBehaviour<,>));
config.AddOpenBehavior(typeof(UnhandledExceptionBehaviour<,>));
});
Hey @neozhu! Which MediatR version do you use? 12? If so, does your ValidationBehaviour work?
Hey @neozhu! Which MediatR version do you use? 12? If so, does your ValidationBehaviour work?
yes, in ver 12. now it's working,VaildationBehaviour throw vaildationexpection, and ValidationExceptionHandler going to do
Very interesting... I have exactly the same ValidationBehavior as yours, and added MediatR and validation rules like this:
builder.Services.AddMediatR(cfg =>
{
cfg.RegisterServicesFromAssemblyContaining<Startup>();
cfg.AddOpenBehavior(typeof(RequestExceptionProcessorBehavior<,>));
cfg.AddOpenBehavior(typeof(ValidationBehavior<,>));
});
builder.Services.AddValidatorsFromAssemblyContaining<Startup>();
However, ValidationBehavior doesn't work... Can't figure out what's wrong.
Check your genetic constraints. The request types changed and if you didn't fix them per the migration guide your behavior will (silently) not be resolved.
Changing constraint of my ValidationBehavior from
where TRequest : IRequest<TResponse>
to
where TRequest : IBaseRequest
solved my problem. Everything else according to the migration guide has been changed earlier. Thanks @jbogard!
I need your help to solve my problem.
I create a ValidationExceptionHandler for ValidationException
After testing, this ValidationExceptionHandler does not subscribe to handle the ValidationException exception
Why? How should i do to get it to work
Thank you.