Closed mmarquesbr closed 1 year ago
It happened to me when I updated the FluentValidation nuget package and look for solution on google. I found none, so I decided to downgrade the FluentValidation version.
Yes, it's a known problem (https://github.com/fullstackhero/dotnet-webapi-boilerplate/issues/639). I've started the work here https://github.com/fullstackhero/dotnet-webapi-boilerplate/pull/693. That PR can use some help ;-)
FluentValidation.DependencyInjectionExtensions -> 11.5.1 FluentValidation.AspNetCore -> 11.3.0
This issue no longer exists. Tested this on the dotnet-7 branch with the latest FV packages.
I found the solution. I found this code on github https://github.com/salt-repositories/din-api/blob/97edd4b02a173dc6881cc19b12447c6154a94b98/src/Din.Domain.Middlewares/Mediatr/FluentValidationMiddleware.cs#L30
update to the latest FluentValidation packages: FluentValidation.AspNetCore Version="11.3.0" FluentValidation.DependencyInjectionExtensions Version="11.9.0"
public class ValidationBehavior<TRequest, TResponse>(IEnumerable<IValidator
var failures = (await Task.WhenAll(validators
.Select(validator => validator.ValidateAsync(context, cancellationToken))).ConfigureAwait(false))
.SelectMany(result => result.Errors)
.Where(failure => failure != null).ToList();
if (failures.Any())
{
var arr = failures.Select(x => $"{Environment.NewLine} {x.ErrorMessage}");
throw new Exception(string.Concat(arr));
}
return await next();
}
}
Describe the bug When I try to add an entity, it causes an error related to automatic validation
{ "messages": [ "Validator \"CreateBrandRequestValidator\" can't be used with ASP.NET automatic validation as it contains asynchronous rules. ASP.NET's validation pipeline is not asynchronous and can't invoke asynchronous rules. Remove the asynchronous rules in order for this validator to run." ], "source": "FluentValidation.AbstractValidator`1", "exception": "Validator \"CreateBrandRequestValidator\" can't be used with ASP.NET automatic validation as it contains asynchronous rules. ASP.NET's validation pipeline is not asynchronous and can't invoke asynchronous rules. Remove the asynchronous rules in order for this validator to run.", "errorId": "d5d95cd8-69a2-499e-a26f-71451765ab57", "supportMessage": "Provide the ErrorId to the support team for further analysis.", "statusCode": 500 }
To Reproduce Steps to reproduce the behavior:
Post to URL: {{url}}/v1/brands Payload: { "name":"Bra1nod #29", "description":"Something Cool!" }
Error appears.
Remarks: This issue is related to FluentValidation.AspNetCore and FluentValidation.DependencyInjectionExtensions, which was bumped to v11.1.0 in my machine, back to v10.4.0 works normally.