Question
How to compute byte size of list<T> in C#
I want to send a large list of objects to another web service for intake. The web service has a byte limit of 6Mb. I want to send my list of 7,000+ objects in 5Mb "batches."
To do this, I need to compute the byte size of the objects and send a batch when the total bytes reaches 5Mb. The issue is that each object can have multiple child objects, and each child objects can have multiple child objects too.
The batch of objects is first serialized to Json before being sent in the body of the request.
Is there a way of computing the byte size of each object at runtime and then adding the value to a running total?
The code snippet below shows what I have to send the list:
jsonList = JsonConvert.SerializeObject(list, 0);
request = new HttpRequestMessage(HttpMethod.Post, url);
request.Content = new StringContent(jsonList , Encoding.UTF8, "application/json");
request.Method = HttpMethod.Post;
request.Headers.Add("Authorization", "Bearer " + authToken);
response = await client.SendAsync(request);
jsonResponse = await response.Content.ReadAsStringAsync();