title: Task.WaitAll hanging with multiple awaitable tasks in ASP.NET
body: <p>Below is a simplified version of the code I'm having trouble with.  When I run this in a console application, it works as expected.  All queries are run in parallel and <code>Task.WaitAll()</code> returns when they are all complete.</p>
  
  <p>However, when this code runs in a web application, the request just hangs.  When I attach a debugger and break all, it shows that execution is wait on <code>Task.WaitAll()</code>.  And the first task has completed, but the others never finish.  </p>
  
  <p>I can't figure out why it hangs when running in ASP.NET, but works fine in a console application.</p>
  
  <pre><code>public Foo[] DoWork(int[] values)
  {
      int count = values.Length;
      Task[] tasks = new Task[count];
  
      for (int i = 0; i &lt; count; i++)
      {
          tasks[i] = GetFooAsync(values[i]);
      }
  
      try
      {
          Task.WaitAll(tasks);
      }
      catch (AggregateException)
      {
          // Handle exceptions
      }
  
      return ...
  }
  
  public async Task&lt;Foo&gt; GetFooAsync(int value)
  {
      Foo foo = null;
  
      Func&lt;Foo, Task&gt; executeCommand = async (command) =&gt;
      {
          foo = new Foo();
  
          using (SqlDataReader reader = await command.ExecuteReaderAsync())
          {
              ReadFoo(reader, foo);
          }
      };
  
      await QueryAsync(executeCommand, value);
  
      return foo;
  }
  
  public async Task QueryAsync(Func&lt;SqlCommand, Task&gt; executeCommand, int value)
  {
      using (SqlConnection connection = new SqlConnection(...))
      {
          connection.Open();
  
          using (SqlCommand command = connection.CreateCommand())
          {
              // Set up query...
  
              await executeCommand(command);
  
              // Log results...
  
              return;
          }
      }           
  }
  </code></pre>