simbolo simbolo - 11 months ago 97
C# Question

WebAPI PUT InsufficientExecutionStackException with DbGeography Type

When handling a PUT call, a WebAPI handler seems to go into a stack overflow type situation when validating the model. The exception isn't clear, and there is no indication as to what in the model is causing this validation class to go into a loop. Attaching the debugger does nothing. The handler will never get called, the serializer will deserialize the posted json normally without incident. What could be wrong?

The following code just loops several hundred times before exiting throwing the exception"

Insufficient stack to continue executing the program safely. This can
happen from having too many functions on the call stack or function on
the stack using too much stack space.

at System.Runtime.CompilerServices.RuntimeHelpers.EnsureSufficientExecutionStack()
at System.Web.Http.Validation.DefaultBodyModelValidator.ValidateNodeAndChildren(ModelMetadata metadata, ValidationContext validationContext, Object container, IEnumerable`1 validators)
at System.Web.Http.Validation.DefaultBodyModelValidator.ValidateProperties(ModelMetadata metadata, ValidationContext validationContext)

The Model is akin to this simple example. The model has default values which I can confirm are all initialized. The model also has no references to itself.

public class Example {
public string test {get; set;}

public CustomEnumType myEnum {get; set;}

Answer Source

Found solution in: Exclude a type from model validation

One of the types on this model is DbGeography. The default validator for some reason gets stuck in a loop inside this type enumerating it's properties. The validator shouldn't even be in there and seems to be a bug. But the behaviour can be overwridden with a custom validator set to ignore this type.