Paralell.ForEach with HttpClient and ContinueWith

I have a method that tries to load data from multiple URLs into Parallel and returns IEnumerabledeserialized types

The method is as follows:

    public IEnumerable<TContent> DownloadContentFromUrls(IEnumerable<string> urls)
    {
        var list = new List<TContent>();

        Parallel.ForEach(urls, url =>
        {
            lock (list)
            {
                _httpClient.GetAsync(url).ContinueWith(request =>
                {
                    var response = request.Result;
                    //todo ensure success?

                    response.Content.ReadAsStringAsync().ContinueWith(text =>
                    {
                        var results = JObject.Parse(text.Result)
                            .ToObject<IEnumerable<TContent>>();

                        list.AddRange(results);
                    });
                });
            }
        });

        return list;
    }

In my unit test (I end up _httpClient to return a known typing), I basically get

The sequence contains no elements.

This is because the method returns before tasks are completed.

If I add .Wait () to the end of my .ContinueWith () calls, it will pass, but I'm sure I'm using the API incorrectly here ...

+4
source share
1 answer

, HttpClient.GetAsync, :

public IEnumerable<TContent> DownloadContentFromUrls<TContent>(IEnumerable<string> urls)
{
    var queue = new ConcurrentQueue<TContent>();

    using (var client = new HttpClient())
    {
        Task.WaitAll(urls.Select(url =>
        {
            return client.GetAsync(url).ContinueWith(response =>
            {
                var content = JsonConvert.DeserializeObject<IEnumerable<TContent>>(response.Result.Content.ReadAsStringAsync().Result);

                foreach (var c in content)
                    queue.Enqueue(c);
            });
        }).ToArray());
    }

    return queue;
}

, Url, GetAsync/Deserialize. , Url Json TContent. - , .

+7

All Articles