title: How to wait for a Task<T> to complete in Web API controller
body: <p>I'm trying to consume a method that returns an async task. The method is:</p>
  
  <pre><code>    public async Task&lt;DataResponse[]&gt; GetGridDataAsync(DataRequest[] requestCollection, object state)
      {
          return await Task.Factory.FromAsync&lt;DataResponse[]&gt;(BeginGetDataAsync(requestCollection, ar =&gt; EndGetDataAsync(ar), state), 
              EndGetDataAsync);
      }
  </code></pre>
  
  <p>When consuming it in a Web API controller, I get the following error 90% of the time:</p>
  
  <blockquote>
    <p>An asynchronous module or handler completed while an asynchronous
    operation was still pending.</p>
  </blockquote>
  
  <p>My attempt to consume it is:</p>
  
  <pre><code>    [HttpGet]
      [Route("GetGridDataAsync")]
      public IEnumerable&lt;DataResponse&gt; GetGridDataAsync()
      {
          var proxy = new Proxy();
          var task = proxy.GetGridDataAsync(dataRequest, new object());
  
          if (!task.IsCompleted)
          {
              task.Start();
              task.Wait();                
          }                        
  
          //var x = (IAsyncResult)task; //i also tried this
          //task.Start();
          //x.AsyncWaitHandle.WaitOne();
  
          return task.Result;
      }
  </code></pre>
  
  <p>How can I block the thread until the request completes (or at least specify a timeout)? I'm going to consume this endpoint from Angular.</p>
  
  <p>This is a follow up to my previous question: <a href="https://stackoverflow.com/questions/25127183/have-web-api-controller-wait-for-iasyncresult-before-completing">Have Web API controller wait for IAsyncResult before completing?</a></p>
  
  <p><strong>Update 1:</strong>
  The exception is being thrown by return proxy.GetGridDataAsync(dataRequest, new object()). I can step through the BeginXXX function but my break point in EndXXX never gets hit. Not sure why. </p>
  
  <p>On occasion I don't get the "asynchronous module or handler..." error above, and get a different error with stacktrace. But I'm not sure what it means:</p>
  
  <pre><code>{  
  "Message":"An error has occurred.",
  "ExceptionMessage":"Object cannot be null.",
  "ExceptionType":"System.ArgumentNullException",
  "StackTrace":" at 
  System.Threading.ThreadPool.RegisterWaitForSingleObjectNative(WaitHandle waitHandle, Object state, UInt32 timeOutInterval, Boolean executeOnlyOnce, RegisteredWaitHandle registeredWaitHandle, StackCrawlMark&amp; stackMark, Boolean compressStack)
  at System.Threading.ThreadPool.RegisterWaitForSingleObject(WaitHandle waitObject, WaitOrTimerCallback callBack, Object state, UInt32 millisecondsTimeOutInterval, Boolean executeOnlyOnce, StackCrawlMark&amp; stackMark, Boolean compressStack)
  at System.Threading.ThreadPool.RegisterWaitForSingleObject(WaitHandle waitObject, WaitOrTimerCallback callBack, Object state, Int32 millisecondsTimeOutInterval, Boolean executeOnlyOnce)
  at System.Threading.Tasks.TaskFactory`1.FromAsyncImpl(IAsyncResult asyncResult, Func`2 endFunction, Action`1 endAction, TaskCreationOptions creationOptions, TaskScheduler scheduler, StackCrawlMark&amp; stackMark)
  at System.Threading.Tasks.TaskFactory.FromAsync[TResult](IAsyncResult asyncResult, Func`2 endMethod)
  at proxy.d__1.MoveNext() in proxy.cs:line 30
  --- End of stack trace from previous location where exception was thrown ---
  }
  </code></pre>