title: .net Core Parallel.ForEach issues
body: <p>I've switched over to .net Core for some projects and am now having a problem with Parallel.ForEach.  In the past I often had a List of id values which I then would use to make web requests in order to get the full data.  It would look something like this:</p>
  
  <pre><code>Parallel.ForEach(myList, l =&gt;
  {
      // make web request using l.id 
      // process the data somehow
  });
  </code></pre>
  
  <p>Well, in .net Core the web requests must all be tagged <code>await</code> which means the Parallel.ForEach action must be tagged with <code>async</code>.  But, tagging a Parallel.ForEach action as <code>async</code> means we have a <code>void async</code> method which causes issues.  In my particular case that means the response returns to the application <em>before</em> all of the web requests in the Parallel loop are completed which is both awkward and causes errors.</p>
  
  <p>Question:  What are the alternatives to using Parallel.ForEach here?</p>
  
  <p>One possible solution I found was to wrap the Parallel loop inside of a Task and await the task:</p>
  
  <pre><code>await Task.Run(() =&gt; Parallel.ForEach(myList, l =&gt;
  {
      // stuff here
  }));
  </code></pre>
  
  <p>(found here:<a href="https://stackoverflow.com/questions/19102966/parallel-foreach-vs-task-run-and-task-whenall">Parallel.ForEach vs Task.Run and Task.WhenAll</a>)</p>
  
  <p>But, that isn't working for me.  When I use that I still end up returning to the application before the loop is completed.  </p>
  
  <p>Another option:</p>
  
  <pre><code>var tasks = new List&lt;Task&gt;();
  foreach (var l in myList)
  {
      tasks.Add(Task.Run(async () =&gt;
      {
           // stuff here
      }));
  }
  await Task.WhenAll(tasks);
  </code></pre>
  
  <p>This appears to work, but is that the only option?  It seems that the new .net Core has rendered Parallel.ForEach virtually useless (at least when it comes to nested web calls).</p>
  
  <p>Any assistance/advice is appreciated.</p>