How to pass a large list of objects to API in C#?

Asked

Viewed 932 times

4

I am passing a list of objects to my API in C#. When the list size is smaller, everything happens as it should. When the list is a little longer the list arrives empty in my API.

When the list is smaller I get the expected result:

Com uma lista menor dá tudo certo

Yet when I send that list:

Array mandado

I get this result:

Lista de Objetos vazia

Could someone explain to me why and how to solve this problem?

  • your API is being published by IIS?

  • Yes, it’s being executed by the IIS

2 answers

1

Try sending it that way:

public JsonResult PdfSellout(List<SellOut> sellout)
{
        var lista = buscarLista();
        var jsonResult = Json(lista, JsonRequestBehavior.AllowGet);
        jsonResult.MaxJsonLength = int.MaxValue;
        return jsonResult;
}

Edit: The above method is for reply, for sending files use the following:

var httpClient = new HttpClient();

httpClient.DefaultRequestHeaders.TransferEncodingChunked = true;

var content = new CompressedContent(new StreamContent(new FileStream("c:\\big-json-file.json",FileMode.Open)),"UTF8");

var response = httpClient.PostAsync("http://example.org/", content).Result;

for a large string use this link

for a list of objects, use this link

1


You can modify the element httpRuntime in the archive web config. adding the following attribute. This should solve the problem of the object having null value when executing the POST.

// limite para o buffer de fluxo de entrada, em kilobytes
<httpRuntime maxRequestLength="2147483647" />

However, you may have problems with the requisition filtering introduced in IIS version 7.0. More specifically (my translation):

When request filtering blocks an HTTP request, IIS 7 will return an HTTP 404 error to the client and log HTTP status with one substatus unique that identifies the reason why the request was denied.

+----------------+------------------------------+
| HTTP Substatus |         Descrição            |
+----------------+------------------------------+
|          404.5 | Sequência URL Negada         |
|          404.6 | Verbo Negado                 |
|          404.7 | Extensão de arquivo Negada   |
|          ...   |                              |

To resolve this limitation, you can modify the element <requestLimits> inside the archive web config.

// limita o POST para 10MB, query string para 256 chars, url para 1024 chars
<requestLimits maxQueryString="256" maxUrl="1024" maxAllowedContentLength="102400000" />

Browser other questions tagged

You are not signed in. Login or sign up in order to post.