3

I have implemented an endpoint in ASP.NET Core 3.1 to retrieve a large JSON object, and I'm having an issue when this JSON starts to be relatively large. I start to have issues around 5~600KB.

For requests that have a body lower than 600~500KB, everything works fine.

The endpoint is defined like this:

[DisableRequestSizeLimit]
[RequestFormLimits(ValueCountLimit = int.MaxValue)]
[HttpPost]
[Route("MyTestEndpoint")]
public void PostTest([FromBody]object objVal)
{
    // objVal is null when the post is larger than ~5~600KB
    string body = objVal.toString;
    ....
}

web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
<system.web>
    <httpRuntime  maxRequestLength="1048576" />  
</system.web>

    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\ApiSLPCalendar.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
      <security>
        <requestFiltering>
          <!-- This will handle requests up to 100MB -->
          <requestLimits maxAllowedContentLength="1048576000" />
        </requestFiltering>
      </security>    
</system.webServer>

<system.web.extensions>
       <scripting>
           <webServices>
               <jsonSerialization maxJsonLength="50000000"/>
           </webServices>
       </scripting>
</system.web.extensions>

  </location>
</configuration>

Startup.cs:

public void ConfigureServices(IServiceCollection services)
{

            services.AddControllers().ConfigureApiBehaviorOptions(options =>
            {
                options.SuppressConsumesConstraintForFormFileParameters = true;
                options.SuppressInferBindingSourcesForParameters = true;
                options.SuppressModelStateInvalidFilter = true;
                options.SuppressMapClientErrors = true;
            });
            services.AddMvc(options =>
            {
                options.MaxModelBindingCollectionSize = int.MaxValue;
            });

            services.Configure<FormOptions>(x =>
            {
                x.ValueLengthLimit = int.MaxValue;
                x.MultipartBodyLengthLimit = int.MaxValue;
            });
....
}

Do you have any clue why this happens?

******* EDIT *******

I've tried several changes but none of them has worked fine.

Finally, following the discussion on FromBody string parameter is giving null I've modified the method, defining a model, removing the generic object, as a parameter. The front-end has been modified to send a key="value", where "Value" represents a stringified JSON object.

Giox
  • 4,785
  • 8
  • 38
  • 81

1 Answers1

6

Maybe you are not hitting the request size limit but the form size limit.please try [RequestFormLimits(MultipartBodyLengthLimit = 104857600)] MultipartBodyLengthLimit is of long type and considering your case.

If you need to upload file size unlimited:-

services.Configure<FormOptions>(x =>
{
    x.ValueLengthLimit = int.MaxValue;
    x.MultipartBodyLengthLimit = int.MaxValue; 
});

when the request hits the IIS then it will search for the web.config to check the max upload length.

//clarify code

<!-- 1 GB -->
 <requestLimits maxAllowedContentLength="1073741824" />

//clarify code

UPDATE

[HttpPost]
[RequestFormLimits(ValueCountLimit = int.MaxValue)]
[Route("MyTestEndpoint")]
public void PostTest([FromBody]object objVal)
{
    ....
    string body = objVal.toString;
    ....
}


services.Configure<FormOptions>(options =>
            {
                options.ValueCountLimit = 10; 
                options.ValueLengthLimit = int.MaxValue; 
                options.MultipartBodyLengthLimit = long.MaxValue; 
                options.MemoryBufferThreshold = Int32.MaxValue;
            });
            services.AddMvc(options =>
            {
                options.MaxModelBindingCollectionSize = int.MaxValue;
            });

Web.Config

<system.web>
    <httpRuntime  maxRequestLength="1048576" />  
</system.web>
    <system.webServer>
  <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="2147483648" />
    </requestFiltering>
  </security>
</system.webServer>
<system.web.extensions>
       <scripting>
           <webServices>
               <jsonSerialization maxJsonLength="50000000"/>
           </webServices>
       </scripting>
   </system.web.extensions>

Pritom Sarkar
  • 2,154
  • 3
  • 11
  • 26
  • I have tried to add the decorator over the endpoint method, [RequestFormLimits(MultipartBodyLengthLimit = 104857600)], but nothing has changed I still receive an emty object when the size is greater than 5~600 KB – Giox Jun 21 '21 at 13:51
  • @Giox I see your parameter is ```objVal``` .please tell me it's binding single or collection velue? you obiously use this **IFormFile** type.because it's file process upload. you can take another parameter for find collection form model.but for upload file.,you should use extra paramter for file.you should use IFromFile type parameter for upload file.hope now you understand.and then it's works fine – Pritom Sarkar Jun 21 '21 at 14:20
  • I'm not uploading a file, but I'm sending a JSON string, which could be very long depending on the content produced. – Giox Jun 21 '21 at 14:35
  • @Giox Check my updated answer now.I think it should work. – Pritom Sarkar Jun 21 '21 at 15:01
  • nothing, I've tried applying all your suggestions but the parameter objVal is always null when the request is larger than 500KB – Giox Jun 21 '21 at 15:17
  • @Giox please carefully check the updated answer and your all code.it should work I think. – Pritom Sarkar Jun 21 '21 at 15:23
  • Updated solution help me to solve my problem – Yogesh Oct 03 '22 at 05:56