0

From the moment I want to deserialize a NodaTime LocalTime in a WebApi request, I get a InsufficientExecutionStackException... (LocalDate works fine)

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

Sample request and exception: http://prntscr.com/8emw3q

Code that reproduces the problem (literal copy paste that results in screenshot above):

public class TimeController : ApiController
{
    [HttpPost, Route("api/time")]
    public IHttpActionResult Post(Req req)
    {
        return Ok(new {test = "ok"});
    }
}

public class Req
{
    public LocalTime Time { get; set; }
}  
Tom Deleu
  • 1,177
  • 1
  • 15
  • 29

1 Answers1

1

There is an issue in NodaTime bugtracker about that: https://github.com/nodatime/nodatime/issues/249. It seems problem is not solved yet, but here is the workaround:

public class NodaTimeFixingBodyModelValidator : DefaultBodyModelValidator {
    public override bool ShouldValidateType(Type type) {
        return type != typeof(LocalTime) // you may want to exclude other noda types here
            && base.ShouldValidateType(type);
    }
}
...
var config = new HttpConfiguration();                        
config.Services.Replace(typeof(IBodyModelValidator), new NodaTimeFixingBodyModelValidator());
...

This basically excludes LocalTime properties from validation (because validation of them is what causes the problem above).

If that is not an option for you, there are two ways:

  1. In version 2.0 of NodaTime, they removed circular references in LocalTime which caused the problem. So you can use 2.0 version of the library (it's in alpha stage still, but can be installed via nuget), or wait until it will be released.

  2. Get rid of noda time properties in your request\response models, since the same can be achieved in other ways.

Evk
  • 98,527
  • 8
  • 141
  • 191