title: Fan-out computation in WebApi using async / await
body: <p>I have say an interface called:</p>
  
  <pre><code>interface IExecutor {
     String Name { get; }
     Task&lt;int&gt; Execute();
  }
  </code></pre>
  
  <p>And two implementations of it (details are irrelevant and you can assume methods are marked with async and work as expected). Each implementation takes between 2-3 seconds to run and there will ever be between 2 and 5 implementations of IExecutor</p>
  
  <p>I have a controller that needs to run all executors and return the results in as ExecutorResult objects. Where ExecutorResult is:</p>
  
  <pre><code>class ExecutorResult {
      int Result; // assume usual get n set
      String ExecutorName;
  }
  </code></pre>
  
  <p>Ideally this should happen in a Fan-Out approach.</p>
  
  <p>I have thought of the following approaches:</p>
  
  <pre><code>List&lt;Task&lt;int&gt;&gt; tasks = new List&lt;Task&lt;int&gt;&gt;();
  foreach(var executor in executors) {
      tasks.Add(executor.Execute());
  }
  
  var results = Task.WhenAll(tasks);
  </code></pre>
  
  <p>The problem I have with this approach is that I m not sure if this is best practice in an ASP WebAPI application. Also - given I would like to return Result objects - where Result needs an the name of the executor and the int result from Execute the above solution doesn't work as outside of the for loop I no longer have access to the Name property of each executor.</p>
  
  <p>So what is the best practice for this approach (again - given a Web Api application and not a Console app)</p>