title: Why does this async action hang when I try and access the Result property of my Task?
body: <p>I have a multi-tier .Net 4.5 application calling a method using C#'s new <code>async</code> and <code>await</code> keywords that just hangs and I can't see why.</p>
  
  <p>At the bottom I have an async method that extents our database utility <code>OurDBConn</code> (basically a wrapper for the underlying <code>DBConnection</code> and <code>DBCommand</code> objects):</p>
  
  <pre><code>public static async Task&lt;T&gt; ExecuteAsync&lt;T&gt;(this OurDBConn dataSource, Func&lt;OurDBConn, T&gt; function)
  {
      string connectionString = dataSource.ConnectionString;
  
      // Start the SQL and pass back to the caller until finished
      T result = await Task.Run(
          () =&gt;
          {
              // Copy the SQL connection so that we don't get two commands running at the same time on the same open connection
              using (var ds = new OurDBConn(connectionString))
              {
                  return function(ds);
              }
          });
  
      return result;
  }
  </code></pre>
  
  <p>Then I have a mid level async method that calls this to get some slow running totals:</p>
  
  <pre><code>public static async Task&lt;ResultClass&gt; GetTotalAsync( ... )
  {
      var result = await this.DBConnection.ExecuteAsync&lt;ResultClass&gt;(
          ds =&gt; ds.Execute("select slow running data into result"));
  
      return result;
  }
  </code></pre>
  
  <p>Finally I have a UI method (an MVC action) that runs synchronously:</p>
  
  <pre><code>Task&lt;ResultClass&gt; asyncTask = midLevelClass.GetTotalAsync(...);
  
  // do other stuff that takes a few seconds
  
  ResultClass slowTotal = asyncTask.Result;
  </code></pre>
  
  <p>The problem is that it hangs on that last line forever. It does the same thing if I call <code>asyncTask.Wait()</code>. If I run the slow SQL method directly it takes about 4 seconds.</p>
  
  <p>The behaviour I'm expecting is that when it gets to <code>asyncTask.Result</code>, if it's not finished it should wait until it is, and once it is it should return the result.</p>
  
  <p>If I step through with a debugger the SQL statement completes and the lambda function finishes, but the <code>return result;</code> line of <code>GetTotalAsync</code> is never reached.</p>
  
  <p>Any idea what I'm doing wrong?</p>
  
  <p>Any suggestions to where I need to investigate in order to fix this?</p>
  
  <p>Could this be a deadlock somewhere, and if so is there any direct way to find it? </p>