Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web API POST parameter is null for large JSON request

I have a POST method in Web API controller that takes a class with 50 fields as parameter. I am getting the parameter value as null in the controller, but if I reduce the number of fields to 30 or so, I am getting the right value.

I have this added to Web.Config:

add key="aspnet:MaxJsonDeserializerMembers" value="140000"

If I use Request.Content.ReadAsStreamAsync(), and use the JsonSerializer to deserialize the stream, I am getting the object with right values.

Is this the preferred way of reading a POST parameter?

like image 398
user636525 Avatar asked Dec 22 '16 02:12

user636525


1 Answers

There were 4 things we had to do for our .NET Web Api project (.NET Framework):

1.In the web.config add:

<system.webServer>
  <security>
      <requestFiltering>
          <requestLimits maxAllowedContentLength="4294967295" />
      </requestFiltering>
  </security>

2. Also add to the web.config:

<system.web>
<httpRuntime targetFramework="4.7.1" maxRequestLength="2147483647" />

3. Large requests need 64 bitness: In Web Api project properties, when running locally in IIS Express, set the bitness to 64: enter image description here When published, make sure the app pool is supporting 64-bit.

4. We noticed the requests hogging up memory for an extended period of time: Have your api controllers implement a base api controller. In that base api controller override the dispose method and dispose of the garbage:

protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);

        GC.Collect();
    }

I do not recommend forcing garbage collection. You should use visual studios built in diagnostics to take snap shots before and after the problems, then compare the memory to see what is eating it up.

like image 72
Jeremy Ray Brown Avatar answered Oct 15 '22 15:10

Jeremy Ray Brown