Other

Async Validation in ASP.NET Core 11

NimblePros Blog
Other

ASP.NET Core 11 makes DataAnnotations validation asynchronous, so a uniqueness check can finally await the database instead of blocking a thread. The post covers the new APIs for minimal APIs and Blazor, plus the concurrency traps that come with them.

Key points

  • New `AsyncValidationAttribute` lets property-level rules call services through `ValidationContext.GetRequiredService<T>()`.
  • `IAsyncValidatableObject` returns `IAsyncEnumerable<ValidationResult>` for rules spanning multiple properties.
  • Minimal APIs with `AddValidation()` run async rules automatically and return 400 problem details on failure.
  • Blazor `EditForm` awaits async validators in every render mode; `RegisterAsyncFieldValidator` powers live field checks.
  • `IsValidationPending` and `IsValidationFaulted` let the UI show checking and failure states while a rule runs.
  • Validators run concurrently, so a scoped `DbContext` can throw unless you switch to `IDbContextFactory<T>`.

Real-world use

  • A registration endpoint that must confirm an email address is not already taken before creating the account.
  • A Blazor registration form that checks username availability as soon as the user leaves the field.
  • A bulk invite endpoint validating a list of emails without triggering one query per item or a concurrent DbContext access.
  • Booking or reservation requests where availability depends on several form fields combined.

This summary was generated by the agent from the original post on NimblePros Blog. The full text and all rights belong to its author and publisher. Go to original

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message