title: MVC4 async and Parallel execution
body: <p>So I'm trying to get my head around this new 'async' stuff in .net 4.5. I previously played a bit with async controllers and the Task Parallel Library and wound up with this piece of code:</p>
  
  <p>Take this model:</p>
  
  <pre><code>public class TestOutput
  {
      public string One { get; set; }
      public string Two { get; set; }
      public string Three { get; set; }
  
      public static string DoWork(string input)
      {
          Thread.Sleep(2000);
          return input;
      }
  }
  </code></pre>
  
  <p>Which is used in a controller like this:</p>
  
  <pre><code>public void IndexAsync()
  {
      AsyncManager.OutstandingOperations.Increment(3);
  
      Task.Factory.StartNew(() =&gt; 
          { 
              return TestOutput.DoWork("1"); 
          })
          .ContinueWith(t =&gt; 
          { 
              AsyncManager.OutstandingOperations.Decrement(); 
              AsyncManager.Parameters["one"] = t.Result; 
          });
      Task.Factory.StartNew(() =&gt;
          {
              return TestOutput.DoWork("2");
          })
          .ContinueWith(t =&gt;
          {
              AsyncManager.OutstandingOperations.Decrement();
              AsyncManager.Parameters["two"] = t.Result;
          });
      Task.Factory.StartNew(() =&gt;
          {
              return TestOutput.DoWork("3");
          })
          .ContinueWith(t =&gt;
          {
              AsyncManager.OutstandingOperations.Decrement();
              AsyncManager.Parameters["three"] = t.Result;
          });
  }
  
  public ActionResult IndexCompleted(string one, string two, string three)
  {
      return View(new TestOutput { One = one, Two = two, Three = three });
  }
  </code></pre>
  
  <p>This controller renders the view in <strong>2 seconds</strong>, thanks to the magic of the TPL.</p>
  
  <p>Now I expected (rather naively) that the code above would translate into the following, using the new 'async' and 'await' features of C# 5:</p>
  
  <pre><code>public async Task&lt;ActionResult&gt; Index()
  {
      return View(new TestOutput
      {
          One = await Task.Run(() =&gt;TestOutput.DoWork("one")),
          Two = await Task.Run(() =&gt;TestOutput.DoWork("two")),
          Three = await Task.Run(() =&gt;TestOutput.DoWork("three"))
      });
  }
  </code></pre>
  
  <p>This controller renders the view in <strong>6 seconds</strong>. Somewhere in the translation the code became no longer parallel. I know async and parallel are two different concepts, but somehow I thought the code would work the same. Could someone point out what is happening here and how it can be fixed?</p>