Skip to content

Instantly share code, notes, and snippets.

@mdekrey
Last active August 29, 2015 14:22
Show Gist options
  • Save mdekrey/59b93ab52cd130c4a2a8 to your computer and use it in GitHub Desktop.
Save mdekrey/59b93ab52cd130c4a2a8 to your computer and use it in GitHub Desktop.
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Mvc.Internal;
namespace Samples.Pages
{
// TODO - check to see if this is still needed by Kestrel
public class KestrelWorkaround
{
readonly RequestDelegate next;
public KestrelWorkaround(RequestDelegate next)
{
this.next = next;
}
public async Task Invoke(HttpContext ctx)
{
var resp = ctx.Response;
// Replace the body stream with a fake one
var realBodyStream = resp.Body;
var fakeBody = new MemoryStream();
resp.Body = new NonDisposableStream(fakeBody);
await next(ctx);
// Thanks to NonDisposableStream, we can just move the position on our MemoryStream
fakeBody.Position = 0;
// Swap the real body stream back in
resp.Body = realBodyStream;
await fakeBody.CopyToAsync(resp.Body);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment