AS am running an ASP.Net core web API from docker container , it throws a validation error :
System.InvalidOperationException: ValidationVisitor exceeded the maximum configured validation depth '32' when validating type 'ClassName'. This may indicate a very deep or infinitely recursive object graph. Consider modifying 'MvcOptions.MaxValidationDepth' or suppressing validation on the model type.
The only place I could find a discussion about this issue is in
here , where it seems that a fix has been provided on the latest version of ASP.net core . I updated my .net core version to the latest , but still facing the same issue.
Here is the code of the class where the validation is causing the issue :
[Required]
[Range(1, long.MaxValue)]
public long Id { get; set; }
[Required(AllowEmptyStrings = false)]
[StringLength(1000)]
public string Name { get; set; }
[Required(AllowEmptyStrings = false)]
[StringLength(200)]
public string Category { get; set; }
[Required(AllowEmptyStrings = false)]
[StringLength(13)]
public string Division { get; set; }
Important : Am the only one facing the issue , as the rest of my team is running the project successfully , any help is highly appreciated.
It is a bug in Asp.Net Core ModelBinding Validation affecting MVC and Web Api https://github.com/dotnet/aspnetcore/issues/13778
One workaround is to increase MaxModelValidationErrors in Startup.ConfigureServices:
services.AddMvc()
.AddMvcOptions(options => {
options.MaxModelValidationErrors = 999999;
})
Increasing MaxModelValidationErrors didn't fix things for me, I had to change a different value (MaxValidationDepth) to get things to work. Wanted to add it here in case anyone had the same problem as I did.
.AddMvcOptions(options =>
{
options.MaxValidationDepth = 999;
});
You could increase MaxModelValidationErrors
in Startup.ConfigureServices:
services.AddControllers(options =>
{
options.MaxModelValidationErrors = 999999;
});
I cannot recommend SuppressChildValidationMetadataProvider anymore.
But you can use this attribute:
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
[ValidateNever]
public List<ToNotValidateSet> ToNotValidateSet { get; set; }
...on all the virtual properties that should not get validated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With