title: Async WebApi ActionFilterAttribute. An asynchronous module or handler completed while an asynchronous operation was still pending
body: <p>I understand await <em>waits</em> for a task (an awaitable) to complete.
  But I'm confused about what that actually means.</p>
  
  <p>The code that <strong>doesn't</strong> work:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public async override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
  {
      if (actionExecutedContext.Response.Content != null)
      {
          var responseContent = await actionExecutedContext.Response.Content.ReadAsStringAsync();
          DoSomething(responseContent);
      }
  }
  </code></pre>
  
  <p>The code that <strong>does</strong> work:
  </p>
  
  <pre><code>public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
  {
      if (actionExecutedContext.Response.Content != null)
      {
          var responseContent = actionExecutedContext.Response.Content.ReadAsStringAsync().ContinueWith(
          task =&gt;
          {
              DoSomething(task.Result);
          });
      }
  }
  </code></pre>
  
  <p>Obviously the error message <strong><em>An asynchronous module or handler completed while an asynchronous operation was still pending.</em></strong> tells me that there was no waiting for the async call to complete but instead the "main" thread continued. I expected the thread to continue but not within the current method. I thought the thread would return to the asp.net stack do some other work and return once the await asyncOperation() operation completed.</p>
  
  <p>I'm using await in other places too - (e.g. waiting for web service responses) - and I didn't run into similar problems anywhere. I wonder why the IActionFilterAttribute behaves differently. In fact my web service calls probably take way longer than reading the content of the response into a string.</p>
  
  <p>Can someone please enlighten me? I have the feeling I didn't understand the concept.</p>