question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Async PropertyValidator Documentation

See original GitHub issue

System 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:closed
  • Created 4 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
JeremySkinnercommented, Dec 27, 2019

essentially I could/should just throw in the IsValid sync method to highlight any bad executions

Yes, that’s correct. I’m actually considering making this the default behavior for 9.0.

Is that applicable to ASP.NET Core as well

Yes. The model binding and validation processes in all versions of ASP.NET Core is synchronous.

If so, not sure how we’re going to work around that

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.

0reactions
benmccallumcommented, Dec 27, 2019

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 a

Edit: Thanks for the further detail re: data annotations being an Achilles heel. Makes sense 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found