title: Is there any way to wait on a Task in an IHttpHandler using task friendly SynchronizationContext?
body: <p>Is there any way in ASP.NET to perform an asynchronous operation in an IHttpHandler and then wait for the task to complete using the new SynchronizationContext enabled through the web.config appsetting <code>UseTaskFriendlySynchronizationContext</code>?</p>
  
  <p>Suppose we have code similar to the following:</p>
  
  <pre><code>public void ProcessRequest(HttpContext context)
  {
     var task = GetAndStartAsyncTask(context);
  
     // do some work
  
     // Wait for task to complete.
     task.Wait();
  
     // etc.
  }
  </code></pre>
  
  <p>Here I would like the any asynchronous callbacks to fire in the current asp.net Synchronization Context. If the setting <code>UseTaskFriendlySynchronizationContext</code> is false this code will work. However, enabling it and the code shown here will deadlock (as far as I can tell because tasks are queued as continuations to the current context, but the first task in the chain (ProcessRequest) is blocking on the Wait).</p>
  
  <p>Is there any way to make this work?</p>
  
  <p>Note: I am using an <code>IHttpHandler</code>, <em>not</em> an <code>IHttpAsyncHandler</code> here.</p>