-2

it's been a while that I try to write asynchronous code in C#, I did it and was sure it is asynchronous, recently I read I checked with postman the time that the function takes to finish when it's Asynchronous and when it's synchronous, and it seems like it takes the SAME time exactly, what I did wrong in my code?

Here is my code:

    [HttpGet]
    [Route("customerslist")]
    public async Task<IHttpActionResult> getData()
    {
        string url1 = @"https://jsonplaceholder.typicode.com/photos";
        string url2 = @"https://jsonplaceholder.typicode.com/comments";
        string url3 = @"https://jsonplaceholder.typicode.com/todos";

        Task<string> firstTask = myHttpCall(url1);
        Task<string> secondTask = myHttpCall(url2);
        Task<string> thirdTask = myHttpCall(url3);

        await Task.WhenAll(firstTask, secondTask, thirdTask);

        var result = firstTask.Result + secondTask.Result + thirdTask.Result;
        return Ok(result);
    }

    private async Task<string> myHttpCall(string path)
    {
        string html = string.Empty;
        string url = path;

        // Simple http call to another URL to receive JSON lists.
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.AutomaticDecompression = DecompressionMethods.GZip;

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        using (Stream stream = response.GetResponseStream())
        using (StreamReader reader = new StreamReader(stream))
        {
            html = reader.ReadToEnd();
        }

        return html;
    }

I make HTTP request to another URL to get their JSON lists, anyone can help me please? I will be happy if anyone can tell me how to write it properly.

Moshe Binieli
  • 343
  • 3
  • 21
  • 3
    Why should the execution time be any different for sync and async? the amount of code executed is the same especially if you are not running any other code. – jdweng Aug 12 '17 at 11:23
  • 3
    There's also nothing async about the `myHttpCall` method, you're not awaiting anything inside it. – DavidG Aug 12 '17 at 11:24
  • 2
    Indeed, you should be getting a warning about an async method with no await... – Jon Skeet Aug 12 '17 at 11:30
  • 3
    @jdweng: If `myHttpCall` where *really* asynchronous, the three requests would be made in parallel, leading to shorter overall time. – Jon Skeet Aug 12 '17 at 11:31
  • Please read your compiler warnings. The C# compiler says: "This async method lacks 'await' operators and will run synchronously." – Stephen Cleary Aug 12 '17 at 13:19
  • it depends on the bandwidth of the channel how much improvement you would have and the response times of the servers. – jdweng Aug 12 '17 at 15:08

1 Answers1

2

Your HTTP calls are synchronous. Use HttpClient in your myHttpCall method more or less like this:

private async Task<string> myHttpCall(string path)
{
    using(HttpClient client = new HttpClient())
    {
           HttpResponseMessage response = await client.GetAsync(path);
           return await response.Content.ReadAsStringAsync();
    }
}

EDIT: To add automatic decompression pass following object to HttpClient constructor:

HttpClientHandler handler = new HttpClientHandler()
{
    AutomaticDecompression = DecompressionMethods.GZip
};
Kedrzu
  • 623
  • 5
  • 12
  • 3
    While it is definitely non-obvious, `HttpClient` is meant to be re-used, not created and disposed every time. We should try to get out of the habit of recommending this pattern with `HttpClient`. – Crowcoder Aug 12 '17 at 11:41
  • Interesting, I didn't know about it. More details [here](https://stackoverflow.com/questions/15705092/do-httpclient-and-httpclienthandler-have-to-be-disposed) – Kedrzu Aug 12 '17 at 11:52