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.