0

Based on This Post - I changed my code because of a slow response when doing a simple Get - it takes about 54-50 seconds. Posting to exactly the same URL using nodejs takes about 10 seconds.

    public class TrayWeighScan
    {
        private static readonly HttpClient httpClient = new HttpClient();
        public async Task<string> doOrder(string orderNumber, decimal weight ) 
        {
            var url = "http://r2hserver/logistics/weighscan?orderNumber="+orderNumber+"&weight="+weight.ToString("F2");
            using (HttpResponseMessage response = await httpClient.GetAsync(url).ConfigureAwait(false))
            {
                response.EnsureSuccessStatusCode(); // throws if 404, 500, etc.
                string responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
                return responseText;
            }
        }
    }

and at the start of my winforms form I have

private TrayWeighScan trayWeighScan = new TrayWeighScan();

Then I call it with this code:

var jsonString = trayWeighScan.doOrder(barcode, Decimal.Parse(weight)).Result;
Martin Thompson
  • 3,415
  • 10
  • 38
  • 62
  • Don't use `.Result`. Use `await`. Especially in a WinForms (or anything else with a UI thread) application. – Andy Jul 17 '22 at 04:46
  • `var jsonString = await trayWeighScan.doOrder(barcode, Decimal.Parse(weight));` and make it `async Task` all the way up. The top-level event handler should be `async void` – Charlieface Jul 17 '22 at 05:03

0 Answers0