0

So I did this project in uni that I am trying to refactor. One of the problems I am having is my method for getting the top list which consist of around 250 movies, e.g. 250 API calls. After that I render them all on my web page. The API I am using is OMDBAPI and I am getting every movie individually as you can see in the code below.

Basically that the web page does is as default loads 10 movies but I can also load in all movies which is around 250.

I am trying to wrap my head around asynchronous programming. So basically it is taking around 4-6 seconds to process this method according to stopwatch in C# but I believe it should be possible to refactor and refine. I am new to asynchronous programming and I have tried looking at MSFT documentation and several issues before here on SO, but I am not getting anywhere with speeding up the calls.

I have looked at using parallel for the issue but I think my problem should be solvable with async? With stopwatch in C# I have pinpointed the delay to come mostly from between the two x.

I would foremost like to speed up the calls but I would love tips on best practice with async programming as well.

public async Task<List<HomeTopListMovieDTO>> GetTopListAggregatedData(Parameter parameter)
{
    List<Task<HomeTopListMovieDTO>> tasks = new List<Task<HomeTopListMovieDTO>>();
    var toplist = await GetToplist(parameter);
//x
    foreach (var movie in toplist)
    {
        tasks.Add(GetTopListMovieDetails(movie.ImdbID));
    }
    var results = Task.WhenAll(tasks);
//x
    var tempToplist = toplist.ToArray();
    for (int i = 0; i < tasks.Count; i++)
    {
        tasks[i].Result.NumberOfLikes = tempToplist[i].NumberOfLikes;
        tasks[i].Result.NumberOfDislikes = tempToplist[i].NumberOfDislikes;
    }

    List<HomeTopListMovieDTO> toplistMovies = results.Result.ToList();

    return toplistMovies;
}
public async Task<HomeTopListMovieDTO> GetTopListMovieDetails(string imdbId)
{
    string urlString = baseUrl + "i=" + imdbId + accessKey;
    return await apiWebClient.GetAsync<HomeTopListMovieDTO>(urlString);
}
public async Task<T> GetAsync<T>(string urlString)
{
    using (HttpClient client = new HttpClient())
    {
        var response = await client.GetAsync(urlString,
            HttpCompletionOption.ResponseHeadersRead);
        response.EnsureSuccessStatusCode();
        var data = await response.Content.ReadAsStringAsync();
        var result = JsonConvert.DeserializeObject<T>(data);
        return result;
    }
}
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
Cebras
  • 1
  • 1
    Hi Cebras. Please take a look at [this](https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client#create-and-initialize-httpclient) guide, regarding the usage of the `HttpClient` class. This class is intended to be instantiated once. – Theodor Zoulias Jan 17 '21 at 13:24
  • Thanks for the comment. I'll look into it! – Cebras Jan 27 '21 at 18:58
  • You can use await response.content.ReadFromJsonAsync(data); – Daniel Feb 11 '23 at 13:40

1 Answers1

0

You async code looks OKey. I would throttle it to not make more than X parallel requests using Partitioner / Parallel for each instead but approach with WaitAll is also good enough unless you see connection refused because of port exhaustion or API DDOS protection.

You should reuse HttpClient, see more details in https://www.aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong, so in your case create HttpClient in the root method and pass it as a parameter to your async methods. HttpClient is thread safe, can be used in parallel calls.

You should dispose HttpResponse.

djbobo
  • 487
  • 2
  • 6
  • For ways to throttle the web requests, look here: [How to limit the amount of concurrent async I/O operations?](https://stackoverflow.com/questions/10806951/how-to-limit-the-amount-of-concurrent-async-i-o-operations/) – Theodor Zoulias Jan 17 '21 at 14:40
  • Thanks I'll look into it – Cebras Jan 27 '21 at 18:58