title: HttpRequestMessage.Content is lost when it is read in a logging DelegatingHandler in ASP.Net Web API
body: <p>When trying to an object in an Action in a Controller it sporadically seems to be null. I discovered that it is due to the <code>ReadAsStringAsync()</code> in the <code>SendAsync()</code> override of the <code>DelegatingHandler</code>. The issue is with the content. When my client sends a content body and it is read in the logger it is never read by the Controller Action Invoker (or may be somewhere in the <code>JsonFormatter</code>). I suspect the subsequent call to <code>Content.ReadAsStringAsync()</code> doesnt throw an exception but also doesnt not return the expected content body (some info is returned stating that the async read is completed).</p>
  
  <p>But my problem remains since I want to read a <code>[FromBody]</code> parameter in an action and it is null when the RaceCondition of <code>Content.ReadStringAsync</code> is won by the <code>DelegatingHandler</code>. When <code>JsonFormatter</code> wins it though, I get the object but that is rare (only at service startup).</p>
  
  <p>Here is my <code>DelegatingHandler</code> code:</p>
  
  <pre><code>public class LogHandler : DelegatingHandler
  {
  
  protected override Task&lt;HttpResponseMessage&gt; SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
      {
          var apiRequest = new WebApiUsageRequest(request);
          WriteLog(apiRequest);
          request.Content.ReadAsStringAsync().ContinueWith(t =&gt;
          {
              apiRequest.Content = t.Result;
              WriteLog(apiRequest);
          });
  
          return base.SendAsync(request, cancellationToken).ContinueWith(task =&gt;
          {
              var apiResponse = new WebApiUsageResponse(task.Result);
              apiResponse.Content = task.Result.Content != null ? task.Result.Content.ReadAsStringAsync().Result : null;
              WriteLog(apiResponse);
              return task.Result;
          });
      }
  }
  </code></pre>
  
  <p>Does anyone have a clue to the solution of this issue?</p>