title: How to inspect MVC response stream using OWIN middleware component?
body: <p>This question has been asked before in a few forms but I cannot get any of the answers to work, I'm losing my hair and unsure if the problem is just that the solutions were from 2 years ago and things have changed.</p>
  
  <p><em><a href="https://stackoverflow.com/questions/26214113/how-can-i-safely-intercept-the-response-stream-in-a-custom-owin-middleware">How can I safely intercept the Response stream in a custom Owin Middleware</a> - I based my code on this, it looks like it should work, but it doesn't</em></p>
  
  <p><em><a href="https://stackoverflow.com/questions/17679644/owin-onsendingheaders-callback-reading-response-body?lq=1">OWIN OnSendingHeaders Callback - Reading Response Body</a> - seems to be a different OWIN version, because method signature doesn't work</em></p>
  
  <p><strong>What I want to do is write an OMC that can inspect the response stream from MVC.</strong></p>
  
  <p>What I did (amongst several other attempts), is to add an OMC that sets context.Response.Body to a MemoryStream, so I can rewind it and inspect what was written by downstream components:</p>
  
  <pre><code>    public async Task Invoke(IDictionary&lt;string, object&gt; env)
      {
  
          IOwinContext context = new OwinContext(env);
  
          // Buffer the response
          var stream = context.Response.Body;
          var buffer = new MemoryStream();
          context.Response.Body = buffer;
          .......
  </code></pre>
  
  <p><strong>What I find is that the MemoryStream is always empty, unless I write to it from another OMC.</strong>  So it seems that downstream OMCs are using my MemoryStream, but MVC responses are not, as if the OWIN pipeline completes before the request goes to MVC, but that's not right is it?</p>
  
  <p>Complete code:</p>
  
  <pre><code>public partial class Startup
  {
      public void Configuration(IAppBuilder app)
      {
          ConfigureAuth(app);
          app.Use(new ResponseExaminerMiddleware());
  
          // Specify the stage for the OMC
          //app.UseStageMarker(PipelineStage.Authenticate);
      }
  
  
  }
  
  
  
  
  public class ResponseExaminerMiddleware
  {
      private AppFunc next;
  
      public void Initialize(AppFunc next)
      {
          this.next = next;
      }
  
      public async Task Invoke(IDictionary&lt;string, object&gt; env)
      {
  
          IOwinContext context = new OwinContext(env);
  
          // Buffer the response
          var stream = context.Response.Body;
          var buffer = new MemoryStream();
          context.Response.Body = buffer;
  
          await this.next(env);
  
          buffer.Seek(0, SeekOrigin.Begin);
          var reader = new StreamReader(buffer);
          string responseBody = await reader.ReadToEndAsync();
  
          // Now, you can access response body.
          System.Diagnostics.Debug.WriteLine(responseBody);
  
          // You need to do this so that the response we buffered
          // is flushed out to the client application.
          buffer.Seek(0, SeekOrigin.Begin);
          await buffer.CopyToAsync(stream);
  
      }
  
  }
  </code></pre>
  
  <p>For what it's worth I also tried a suggestion where the response.Body stream is set to a Stream subclass, just so I could monitor what is written to the stream and bizarrely the Stream.Write method is called, but with an empty byte array, never any actual content...</p>