alkincakiralar
alkincakiralar

Reputation: 33

Post request not working when too large data .net core web api

net core web api project

I got one post request method which must be work with a lot of entities

if i use send 20.000 or 30.000 entities its not a problem method works but if i send too large data example 100.000 80.000 not even falling into method and return not found to the client

what should i do ?

here is my method

[HttpPost]
        [DisableRequestSizeLimit]
        public async Task<IActionResult> ArchivePlugs([FromBody] ArchivePlugs model)
        {
            try
            {   
                 await _archivePlugsRepository.AddSynchronizeAsync(model.Archive_Plugs);
                 await _archivePlugProductRepository.AddSynchronizeAsync(model.Archive_PlugProduct);
                 await _archivePlugTaxsRepository.AddSynchronizeAsync(model.Archive_PlugTax);*/

                return OkResult();
            }
            catch
            {
                return BadRequestResult();
            }
        }

what i have tried ?

[DisableRequestSizeLimit]

services.AddMvc().AddJsonOptions(options =>
            {
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                options.SerializerSettings.ContractResolver = CustomContractResolver.Instance;
            });

and remomve [frombody] tag and use in this method

 using (var reader = new StreamReader(Request.Body))
                {
                    var body = reader.ReadToEnd();

                    ArchivePlugs results = JsonConvert.DeserializeObject<ArchivePlugs>(body);

                }

but none of them works

Upvotes: 3

Views: 5447

Answers (1)

Fredrik Stigsson
Fredrik Stigsson

Reputation: 423

If you are using an IIS server you need to set requestLimits in a web.config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <security>
      <requestFiltering>
        <!-- 4294967295 bytes (4 GiB) is the maximum value-->
        <requestLimits maxAllowedContentLength="52428800" />
      </requestFiltering>
    </security>
  </system.webServer>
</configuration>

You can also set FormOptions in your StartUp class:

public void ConfigureServices(IServiceCollection services)
{
    // Set limits for form options, to accept big data
    services.Configure<FormOptions>(x =>
    {
        x.BufferBody = false;
        x.KeyLengthLimit = 2048; // 2 KiB
        x.ValueLengthLimit = 4194304; // 32 MiB
        x.ValueCountLimit = 2048;// 1024
        x.MultipartHeadersCountLimit = 32; // 16
        x.MultipartHeadersLengthLimit = 32768; // 16384
        x.MultipartBoundaryLengthLimit = 256; // 128
        x.MultipartBodyLengthLimit = 134217728; // 128 MiB
    });

    // Add the mvc framework
    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

    ...

} // End of the ConfigureServices method

Another option is send smaller chunks of ArchivePlugs.

Upvotes: 2

Related Questions