Hi, I\'m working on a project where it is needed to send potentially huge json of some object via HttpClient, a 10-20 mb of JSON is a typical size. In orde
Use PushStreamContent. Rather than have Web API "pull" from a stream, it lets you more intuitively "push" into one.
object value = ...;
PushStreamContent content = new PushStreamContent((stream, httpContent, transportContext) =>
{
using (var tw = new StreamWriter(stream))
{
JsonSerializer ser = new JsonSerializer();
ser.Serialize(tw, value);
}
});
Note that JSON.NET doesn't support async during serialization so while this may be more memory efficient, it won't be very scalable.
I'd recommend trying to avoid such large JSON objects, though. Try to chunk it up, for instance, if you're sending over a large collection. Many clients/servers will flat out reject something so big without special handling.