Async PropertyValidator Documentation
See original GitHub issueSystem Details
- FluentValidation version: 8.4.0 (via FluentValidation.AspNetCore 8.4.0)
- Web Framework version: ASP.NET Core 2.2
Issue Description
We’re currently investigating performance/deadlock issues in our codebase and a use of .GetAwaiter().GetResult()
inside an implementation of a PropertyValidator
’s IsValid
method in an extremely hot path is concerning me.
I’ve looked at #999, but wanted to confirm that the suggestion to use AsyncValidatorBase
will prevent our deadlocking issues. Specifically, that this line is OK: https://github.com/JeremySkinner/FluentValidation/blob/c24d6929f013b4ba003f0c6819c87672b0e984cf/src/FluentValidation/Validators/AsyncValidatorBase.cs#L49
If it’s all g, would be happy to help write up a docs PR on the use of this base class; just let me know.
Cheers, Ben
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Async PropertyValidator Documentation · Issue #1285
GetResult() forces an asynchronous method to run synchronously. This is only ever invoked if you have async validators, but you're calling them ...
Read more >Asynchronous Validation — FluentValidation documentation
By default, FluentValidation allows custom rules defined with MustAsync or CustomAsync to be run asynchronously, as well as defining asynchronous conditions ...
Read more >Class AsyncPredicateValidator
ValidateAsync (PropertyValidatorContext, CancellationToken) ... Performs validation asynchronously. View Source. Declaration. public virtual async ...
Read more >How to await method in PropertyValidator's overridden ...
I'm trying to use my Data Repository in PropertyValidator class. The problem is that method in the Repository is async... and I can't...
Read more >Asynchronous Validation With FluentValidation
FluentValidation is a popular .NET library for building strongly-typed validation rules. It can be used to separate the validation logic from model classes, ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes, that’s correct. I’m actually considering making this the default behavior for 9.0.
Yes. The model binding and validation processes in all versions of ASP.NET Core is synchronous.
You’d have to stop using automatic validation and instead inject the validators into your controllers. If your controller actions are then async, you can then directly invoke the validator’s
ValidateAsync
inside the controller action.Keep in mind that the validation pipeline inside ASP.NET MVC / ASP.NET Core is basically designed entirely with the design of DataAnnotations attributes in mind, which don’t have any kind of concept of async execution. While FluentValidation does its best to plug into the pipeline, it’ll never be 100% perfect as FluentValidation and DataAnnotations are very different, so there will always be limitations like this.
Executing the validators directly will always give you the most control and allow you to fully utilize FluentValidation’s features, while MVC auto validation is more convenient, but also much more limited.
Thanks Jeremy. Kind of puzzling the asp.net core team didn’t go async in the validator pipeline. I’ll do some reading to find out why and if it’s on the horizon.
I think since we are using MediatR, I might introduce a middleware into the pipeline before our handlers that does the validation and uses
ValidateAsync
. I’m sure I won’t be the first to do this so will take aEdit: Thanks for the further detail re: data annotations being an Achilles heel. Makes sense 😃