0

So I had to create dozens of API requests and get json to make it an object and put it in a list. I also wanted the requests to be parallel because I do not care about the order in which the objects enter the list.

  public ConcurrentBag<myModel> GetlistOfDstAsync()
  {
   var req = new RequestGenerator();
   var InitializedObjects = req.GetInitializedObjects();
   var myList = new ConcurrentBag<myModel>();

   Parallel.ForEach(InitializedObjects, async item =>
            {
            RestRequest request = new RestRequest("resource",Method.GET);
            request.AddQueryParameter("key", item.valueOne);
            request.AddQueryParameter("key", item.valueTwo);


            var results = await GetAsync<myModel>(request);

            myList.Add(results);
            });

   return myList;
   }

What creates a new problem, I do not understand how to put them in the list and it seems I do not use a solution that exists in a form ConcurrentBag

Is my assumption correct and I implement it wrong or should I use another solution?

CryptoNight77
  • 377
  • 1
  • 2
  • 12
  • 1
    You can convert a ConcurrentBag to a list using `ToList()`. Does that address the question? – John Wu Feb 18 '19 at 18:58
  • HTTP requests **do not** require use of `Parallel` to run concurrently. It has buil in `Task` support (https://stackoverflow.com/questions/21779206/how-to-use-restsharp-with-async-await) so you should use `Task.WhenAll` on that. – mjwills Feb 18 '19 at 20:26
  • If you really want to persist with using `Parallel`, use something like https://stackoverflow.com/questions/12564465/how-do-you-use-asparallel-with-the-async-and-await-keywords and use `ToList` at the end. – mjwills Feb 18 '19 at 20:29
  • can you show me the way master? – CryptoNight77 Feb 19 '19 at 12:07

1 Answers1

1

I also wanted the requests to be parallel

What you actually want is concurrent requests. Parallel does not work as expected with async.

To do asynchronous concurrency, you start each request but do not await the tasks yet. Then, you can await all the tasks together and get the responses using Task.WhenAll:

public async Task<myModel[]> GetlistOfDstAsync()
{
  var req = new RequestGenerator();
  var InitializedObjects = req.GetInitializedObjects();
  var tasks = InitializedObject.Select(async item =>
  {
    RestRequest request = new RestRequest("resource",Method.GET);
    request.AddQueryParameter("key", item.valueOne);
    request.AddQueryParameter("key", item.valueTwo);
    return await GetAsync<myModel>(request);
  }).ToList();

  var results = await TaskWhenAll(tasks);
  return results;
}
Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810