'ASP.net Core API : ValidationVisitor exceeded the maximum configured validation depth '32'
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.
Solution 1:[1]
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;
});
Solution 2:[2]
You could increase MaxModelValidationErrors in Startup.ConfigureServices:
services.AddControllers(options =>
{
options.MaxModelValidationErrors = 999999;
});
Solution 3:[3]
I am in the process of upgrading a project to .NET 6 and ran into this error message. The above solutions did not fix it. The validation error was referring to a virtual Subproperty and a validated something that I did not even want to be validated at this point.
This fixed it for me:
services.AddMvc(option => {
//fix: max validation depth error in TryValidateModel(model) since .NET6
option.ModelMetadataDetailsProviders.Add(new SuppressChildValidationMetadataProvider(typeof(MyVirtualSubpropertyClassThatShouldNotBeValidated)));
});
Disclaimer: I am well aware that this is a quickfix and potentially has sideeffects. Also I cannot spend a week on properly fixing it and just want a working version for now.
Solution 4:[4]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Christopher Rice |
| Solution 2 | Constantine Nikolsky |
| Solution 3 | CodingYourLife |
| Solution 4 | CodingYourLife |
