title: Async functions in C# deadlock when called from javascript aspx page via PageMethods
body: <p><strong>Problem Summary:</strong><br/>
  I'm trying to use PageMethods to call a C# function from an HTML page.  The problem is that the C# function I'm calling is marked as async and will await the completion of other functions.  When the nested async C# functions are called by PageMethods, the C# code seems to deadlock.<br/>
  I've given an example ASP.NET page with C# coded behind it to illustrate the idiom I'm trying to use.</p>
  
  <p><strong>Example WebForm1.aspx</strong><br/></p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication3.WebForm1" %&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;&lt;title&gt;&lt;/title&gt;&lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
          &lt;asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"/&gt;
          &lt;div&gt;
              &lt;input type="button" value="Show Function timing" onclick="GetTiming()"/&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  
  &lt;script type="text/javascript"&gt;
      function GetTiming() {
          console.log("GetTiming function started.");
          PageMethods.GetFunctionTiming(
              function (response, userContext, methodName) { window.alert(response.Result); }
          );
          console.log("GetTiming function ended."); // This line gets hit!
      }
  &lt;/script&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>Example WebForm1.aspx.cs</strong><br/></p>
  
  <pre><code>using System;
  using System.Threading.Tasks;
  using System.Diagnostics;
  using System.Web.Services;
  using System.Web.UI;
  namespace WebApplication3
  {
      public partial class WebForm1 : Page
      {
          protected void Page_Load(object sender, EventArgs e) { }
          [WebMethod]
          public static async Task&lt;string&gt; GetFunctionTiming()
          {
              string returnString = "Start time: " + DateTime.Now.ToString();
              Debug.WriteLine("Calling to business logic.");
  
              await Task.Delay(1000); // This seems to deadlock
              // Task.Delay(1000).Wait(); // This idiom would work if uncommented.
  
              Debug.WriteLine("Business logic completed."); // This line doesn't get hit if we await the Task!
              return returnString + "\nEnd time: "+ DateTime.Now.ToString();
          }
      }
  }
  </code></pre>
  
  <p><strong>Question:</strong><br/>
  I absolutely need to be able to call asynchronous code from my web page UI.  I'd like to use async/await functionality to do this, but I haven't been able to figure out how to.  I'm currently working around this deficit by using <code>Task.Wait()</code> and <code>Task.Result</code> instead of async/await, but that's apparently not the recommended long-term solution.<br/>
  <em>How can I await on server-side async functions in the context of a PageMethods call???</em> <br/>
  <em>I really, really want to understand WHAT is happening under the covers here, and WHY it does NOT happen when the async method is called from a console app.</em></p>