Last active
August 29, 2015 14:22
-
-
Save mdekrey/59b93ab52cd130c4a2a8 to your computer and use it in GitHub Desktop.
Workaround for https://github.com/aspnet/KestrelHttpServer/issues/103
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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