1

I'm trying to make a method that runs as an async method. The problem is the longest part is not 'awaitable' (not support the 'await' keyword ). I based my code using this link: here.

Some parts in my code have taken from Postman - HTTP POST to an API.

        private async Task<byte[]> PostRequestToOptiRoutesAsync()
        {
        string urlCallBack = u.Trim(this.URLAchzraR4M);
        var client = new RestClient(u.Trim(PrmtrimGlobliim_407_2.Mchrozt));
        client.Timeout = -1;
        var request = new RestRequest(Method.POST);
        request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
        request.AddParameter("hfd_xml", u.Trim(BlovXMLBkshaOPT).Replace("\r", "").Replace("\n", "").Replace("\t", ""));
        request.AddParameter("link", urlCallBack);

        //UNTIL HERE - IT IS SYNC CODE AND NOT RELEVANT 

        //FROM HERE - THE POST TO THE API
        IRestResponse response = client.Execute(request); // client.Execute is not awaitable - THE PROBLEMATIC ROW IS HERE
        return u.UTF8FromAnsi(response.Content); // u.UTF8FromAnsi CONVERT THE RESULT- IGNORE IT
        }

        private async Task GetOptimalRoute()
        {
            Task<byte[]> result =  PostRequestToOptiRoutesAsync();
            await result;
        }

The problem is that when I call GetOptimalRoute() using VS debug, my thread is not immediately moved to the next row, but it acts like sync code. It pauses for few seconds - unlike the code in the link above!! How can I resolve this issue and make this code to be async?

חיים חדד
  • 512
  • 5
  • 17

0 Answers0