0

I have a web API controller with a POST method as follows.

public class MyController : ApiController
{
    // POST: api/Scoring
    public HttpResponseMessage Post([FromBody]ReallyLargeJSONObject request)
    {
        // some processing of this large json object
        return Request.CreateResponse(HttpStatusCode.OK, someResponseObject);
    }
    ....
}

This is consumed by a HTTPClient as follows

HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.BaseAddress = new Uri("http://localhost");
ReallyLargeJSONObject request = new ReallyLargeJSONObject();
var task = httpClient.PostAsJsonAsync("api/my", request)

I have read at a few places that in .NET 4.5, HttpClient class streams the data (and doesn't buffer it). That's great as this way my server will not get overloaded with large packets. However I would like to test this. For this, I have made size of my ReallyLargeJSONObject instance from the client to be ~20MB. I also try with even large packets (~1GB). When I use fiddler, it shows only one request going to server. My questions:

  1. Should I see multiple request going to server in fiddler?
  2. If set breakpoints in the MyController.Post method, should it be hitting multiple times when data is been streamed?
paul deter
  • 857
  • 2
  • 7
  • 20

1 Answers1

1

You should not be seeing multiple requests nor the Post method being hit multiple times as it would be happening at a lower level/method call.

To actually see the chunks broken up and being sent over the wire you can use something like Wireshark to monitor network activity. With this you'll be able to see how long it's taking, how many packets are being used, how big each packet is, etc.

Reference https://www.wireshark.org

Reading on streams: Can you explain the concept of streams?

Reading on packets: https://en.wikipedia.org/wiki/Packet_segmentation

Community
  • 1
  • 1
Sean Wang
  • 778
  • 5
  • 14
  • If POST is not called multiple times, how does the data chunking happens under the hood? The data is chunked and sent multiple times to server, right? – paul deter Aug 12 '16 at 22:49
  • 1
    Yes it happens within the HttpClient code if it is actually streaming the data. On the wire the data will be broken down into packets conforming to TCP. Essentially on the wire the data is broken from one big part into many smaller parts and sent over the network, then at the receiving end it is pieced together and will look like the exact big park that was sent originally from the code. – Sean Wang Aug 12 '16 at 23:07
  • 1
    @pauldeter I have added some addition resources for more insight on these network operations. If you use wireshark and take a look you'll actually be able to see how many packets your data is split into and some interesting under-the-hood action. – Sean Wang Aug 12 '16 at 23:13