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