title: Creating a custom Delegating handler
body: <p>I'm looking to add a custom delegating handler to my asp.net web api. The purpose of this handler would be to inspect the contents of my requests and validate them for certain conditions before they continue on in the request pipeline... I would also want to log out the requests at this entry point.</p>
  
  <p>However, the request.content that is being passed in the pipeline is a system.web.http.webhost.httpcontrollerhandler.lazystreamcontent - which looking up this type in MSDN reveals nothing about how to deserialize or view the request contents - when I try to read the contents from a stream, I'm left with an empty string. see example: </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Net;
  using System.Net.Http;
  using System.Threading;
  using System.Threading.Tasks;
  using System.Web;
  using System.Web.Http;
  
  namespace Haxy.Pencilthis.Api.MVC.Handlers
  {
      public class ContentValidationHandler : DelegatingHandler
      {
  
          private static log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  
          protected override Task&lt;HttpResponseMessage&gt; SendAsync(HttpRequestMessage request,
              CancellationToken cancellationToken)
          {
              Stream strea = new MemoryStream();
              request.Content.CopyToAsync(strea);
              StreamReader reader = new StreamReader(strea);
              String res = reader.ReadToEnd();
              Log.Info("request content: " + res);
              return base.SendAsync(request, cancellationToken);
  
              //
          }
  
      }
  }
  </code></pre>