0

I have the following code in a C# WPF app. This has been working perfectly in testing for a month:

try
{
    string zServerName = "http://" + dispenseDetails.dipenseServerCompName + ":8800/Api/";
    
    var options = new RestClientOptions(zServerName);
    var client = new RestClient(options);
    var request = new RestRequest(endpoint + APIparameter);
    request.Method = apiMethod;
                    
    Debug.WriteLine(zServerName + endpoint + APIparameter);
                   
    request.Method = apiMethod;
                  
    if (!getToken)
    {
        if (string.IsNullOrEmpty(dispenseDetails.curZToken))
        {
            dispenseDetails.curZToken = getNewToken();
            Debug.WriteLine("Got new token: " + dispenseDetails.curZToken);
        }
        else
        {
            Debug.WriteLine("Already have token: " + dispenseDetails.curZToken);
        }
                        
        request.AddHeader("Authorization", "Bearer " + dispenseDetails.curZToken);
    }
                   
    var response = client.ExecuteAsync(request).Result;
    Debug.WriteLine(response);
    if (response.ErrorException != null)
    {
        msgBox.Show(null, "Connection failed. " + response.ResponseStatus);
        return "false";
    }
    if (response.StatusCode == System.Net.HttpStatusCode.OK)
    {
        var JsonString = response.Content;
        Debug.WriteLine(JsonString);
        return JsonString;
    }
    else
    {
        Utility.LogFile("API Fail", response.StatusCode.ToString() + ":" + response.Content, apiMethod + " " + endpoint, "zDispenseServer: " + APIparameter + reqBody, false);
        return "false";
     }
}
catch (Exception exe)
{
    Utility.LogFile(exe.Message, "Critial Error: Cannot contact ZDispense Server", "Api Utility",  "RestAPI",false);
    return "false";
}     

Now I am aware it isn't great code and I'm fixing up on the responses (I'm self taught amateur).

My issue is now when I make this call the comp hangs and waits and gets an answer... 15-> 30 secs later. I understand I'm not using async/await and this will fix the hang, but it has always worked instantly.

  • TESTED: I do exactly the same call (using same user and password and everything) with Postman

Status 200OK Time 40ms

So postman is wokring as per usual.

  • Wireshark I have never used wireshark but installed for this reason.

I tested with Postman and the instant I send the call it appears on Wireshark. 0.03s response time.

Tested with my app/restsharp:

  • 0.03s response time BUT it takes about 15-30 secs to appear in wireshark.

My theory is that the call is fast (eg the call then response) as it shows on WS fast BUT it's like the call sits there and is delayed THEN makes the call...

I have other code and another API which is working perfectly fine using the exact code above but to a different server. The "fast" server is my own nodejs API

This one is a companies API test software on a computer on my local network (home/personal).

I don't know if that matters but thought to mention it.

Could anyone point me in the right direction?

Am I right in saying if it doesnt appear in WS for 15 secs, that means its... "sitting" in Visual Studio before being sent, and if this is the case.... why??

I have no proxies, no weird interesting stuff.

Fresh install of Vis studio because I tried that in case I had some setting screwing things up.

EDIT:

  • It hangs at

    var response = client.ExecuteAsync(request).Result;

Hang might not be the right word, I know it isnt an AWAIT but it used to pause for 0.5 sec but now it hangs for 15-30 secs

GAngel
  • 1
  • 2
  • 2
    First of all, have you debugged your code to see which part is taking the 30 seconds? Secondly, you are calling `.Result` on a `Task` which is really bad practice and can lead to deadloacks. You should be `await`ing those calls properly. – DavidG Jul 11 '22 at 08:40
  • Thanks @DavidG . I did try doing the Await on it but after changing out to Task i get 'RestResponse' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'RestResponse' could be found (are you missing a using directive or an assembly reference?) I've added the hang line above in edit which is also that very line. On my other api's it just sends and responds right away, which this used to, but now it takes long time before it even sends! – GAngel Jul 11 '22 at 10:23
  • @DavidG so had a breakthrough today. Tried code on 3 comps. Mine, spare and the server with the api running. Server was fast, both others slow. I turned off the server firewall and boom, all of it works with no delay. My issue now is that why is there no delay in postman but there is a delay in my code.. eg. why does postman avoid the firewall and then why did this suddenly start?? – GAngel Jul 13 '22 at 08:05

1 Answers1

0

In WPF, you should either make your method async void and await ExecuteAsync or use the latest version with sync methods that were recently brought back. These methods do some advanced stuff with thread scheduling to ensure it doesn't block.

Alexey Zimarev
  • 17,944
  • 2
  • 55
  • 83
  • THanks Alexey for your response and thanks for the advice. I have since been using async. The error ended up being found that the supplier API (desktop database) had done an update which enabled a new firewall protocol which literally blocked their OWN api from functioning. It ended up being an Australia wide issue for their software that I had created the ticket for as they hadnt noticed that functionality had suddenly been affected! – GAngel Jul 27 '22 at 00:40