0

I'm using v3.9.56.0 and I'm encountering a stack overflow exception when I call ToOptimizedResult (Called from my own service runner) on a returned HttpResult from a service. When I dig deeper I found the exception was coming from the JsonSerializer.

Here is the snippet of code for what is being returned:

return new HttpResult(new FileInfo(Path.Combine(path, file)), true)
Scott
  • 21,211
  • 8
  • 65
  • 72
kinstephen
  • 721
  • 5
  • 8

1 Answers1

3

This is happening because ToOptimizedResult expects to receive a DTO response object that it can compress to create a CompressedResult response.

However you are providing an HttpResult which effectively is wrapper for the byte[]/string DTO response object of the file you are loading, this wrapper is a complex type, and isn't really what you want to be trying to optimise.

If your file is binary then you should return the byte[] of the contents, if it is plain text then return a string. Then the ToOptimizedResult can optimise that data.

// Binary file type
return File.ReadAllBytes(Path.Combine(path, file));  // returns byte[]

// Plain text file type
return File.ReadAllText(Path.Combine(path, file));  // returns string
Scott
  • 21,211
  • 8
  • 65
  • 72
  • I kind of thought this might be the issue, but wasn't sure. I wonder if there is a good way to determine when to optimize a HttpResult. – kinstephen Mar 23 '14 at 17:31
  • 1
    @kinstephen You shouldn't ever optimise an `HttpResult`. In most cases DTO object responses should be optimised where possible. It should only be avoided where the client doesn't support optimised formats, but that is checked in the method. – Scott Mar 23 '14 at 18:18