Comments (3)
I ran into the same issue. I debugged into it and recognized that there are two separate validations being made using the same validators. The first one is done by the mvc pipeline using FluentValidation and the ValidatorActionFilter. The second one is done by the mediatR pipeline initiated by ValidationPipelineBehavior while using the same validator objects being injected into it. Taking UsersController.Create as an example:
Before UsersController.Create is called, the mvc validation validates the Conduit.Features.Users.Create.Command object. If it fails ValidatorActionFilter will respond with Statuscode 422. However, if it does not fail, the object is being handed over to the mediatR by calling Send(). Now the second validation issued by ValidationPipelineBehavior is taking place. As this validates the same object using the same validators, the validation will never fail.
So yes in this case, ValidationPipelineBehavior is useless. However, if you pass a different object to mediatR than that being passed to the controller, the second validation makes sence again.
But here, imho, lies the actual problem: ValidationPipelineBehavior throws ValidationExceptions which are not being handled appropriately. Instead, the exception ends up being handled in the ErrorHandlingMiddleware which would respond with Statuscode 500.
TL;DR: ValidationPipelineBehavior makes sence if you pass a different object to mediatR than that being passed to the controller. Instead of throwing ValidationExceptions, I suggest doing the same thing as in ValidatorActionFilter: respond with Statuscode 422 and an appropriate payload explaining the problem.
from aspnetcore-realworld-example-app.
I'm not sure if we should respond with a 422 in this situation. Mediatr is designed to have multiple handlers file for a single command and commands might not always be triggered by a controller. I'm still new with Mediatr but do events use the same pipeline?
from aspnetcore-realworld-example-app.
Good point, I didn't consider the cases where MediatR is used outside of controllers. So maybe the best solution is to throw in ValidationPipelineBehavior as is and explicitly handle ValidationExceptions in ErrorHandlingMiddleware or even a dedicated middleware for this concern. This provides you with the opportunity to handle the ValidationException differently in non-controller situations. However, in such situations, you might not use any validation at all and simply would not provide any validator for this kind of command type.
Btw, MediatR doesn't use PipelineBehaviors when you call Publish() instead of Send(). Just compare NotificationHandlerWrapper and RequestHandlerWrapper.
from aspnetcore-realworld-example-app.
Related Issues (20)
- Problems with deploying HOT 1
- HTTP ERROR 404
- Security Risk - User Hijacking? HOT 4
- Database provider SQlLite hard-coded HOT 3
- Unable to delete article containing comments HOT 2
- Implement one database transaction per HTTP API call HOT 3
- CORS and pre-flight OPTIONS requests. HOT 4
- Missing app.UseAuthentication()? HOT 2
- Class vs Namespace HOT 4
- cannot use chinese for the title of article HOT 1
- Default connection string is hard-coded, only SQLite can be used. HOT 1
- can't connect to localhost:5000 HOT 1
- No License for Repo HOT 2
- Should .dockerignore include .git folder? HOT 2
- Circular references from Article HOT 3
- Wrong docker command for 'make run' in documentation node "Docker Build" HOT 1
- Adding an existing tag when editing an article causes an error
- Badly implemented GET /articles endpoint
- ASP MVC core frontend request
- are there any plans to upgrade this to .Net 6? HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aspnetcore-realworld-example-app.