none
Need performance advice for HttpClient and JSON serialization RRS feed

  • Question

  • Hello all, I'm new to network programming and have just completed my first RESTful client/server "pair". My server is a web api written in ASP.NET core and the client is a full framework WPF application using HttpClient. I'm trying to send an object contaning some simple data types including an array of Bytes. The array is typically about 4MB or so but could be larger. I have it up and running but I'm unhappy with the performance. While I have little experience in these matters I believe the JSON serialization/deserialization is the culprit. Here's what I've got:

    MY data class that's being retrieved by the client:

        public class ByteData
        {
            public int Rows { get; set; }
            public int Columns { get; set; }
            public Byte[] Values { get; set; }
        }

    In my .NET Core server I serve up the data as follows:

            [HttpGet("/api/data")]
            public IActionResult GetImage()
            {
                if(MyServer.IsRunning)
                {
                    return Ok(MyServer.byteData);
                }
                else
                {
                    return NotFound("Not running");
                }
            }

    where "byteData" is an object of type "ByteData". In my WPF client I retrieve the data as follows:

    HttpClient client = new HttpClient();           
    client.BaseAddress = new Uri("http://localhost:8081/");
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    ByteData dataImage = RetrieveData();
    

    where:

            private ByteData RetrieveData()
            {
                ByteData data = null;
                stopWatch.Reset();
                stopWatch.Start();
    
                HttpResponseMessage response = client.GetAsync("api/myData").Result;
                if (response.IsSuccessStatusCode)
                {
                    data = response.Content.ReadAsAsync<ByteData>().Result;
                }
    
                stopWatch.Stop();
                timeLabel.Content = stopWatch.ElapsedMilliseconds.ToString();
    
                return data;
            }

    And typical ByteData object is something like:

    byteData = new ByteData();
    byteData.Rows = 2028;
    byteData.Columns = 2048;
    byteData.Values = new Byte[byteData.Columns * byteData.Rows];

    When I run in release mode with both the client and server running on the same machine (i.e. localhost) my timings are showing about 300ms, which is a little on the slow side since my arrays may be larger. From where I've placed the stopwatch Start/Stop commands it looks like the time is in the JSON serialization/deserialization. Any advice anyone may have on how to speed this up is much appreciated! Thank you in advance.

    -L

    Monday, April 3, 2017 4:51 PM

All replies

  • >>where I've placed the stopwatch Start/Stop commands it looks like the time is in the JSON serialization/deserialization

    For performance between API client and service, I found you have used “response.Content.ReadAsAsync<ByteData>().Result” at client side. As far as I know, the json formatter is the fastest in Asp.net Core server side. There are no settings to speed up the serialization.

    In addition, I think 300ms is ok. I suggest you try to download a large file to check whether the performance is not accepted. 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, April 4, 2017 2:29 AM