Skip to content

Commit

Permalink
Kestrel bug workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed Jul 3, 2016
1 parent 99ea5f4 commit 9d3caa2
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions source/_posts/using-static-content-generation-in-asp.net-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,28 +88,35 @@ public class StaticGeneratorMiddleware
var buffer = new MemoryStream();
var reader = new StreamReader(buffer);
context.Response.Body = buffer;

// execute the rest of the pipeline
await _next(context);

if (context.Response?.ContentType?.Contains("text/html") == false && context.Response.StatusCode != 200)
try
{
// execute the rest of the pipeline
await _next(context);
return;

if (context.Response?.ContentType?.Contains("text/html") == false && context.Response.StatusCode != 200)
{
await _next(context);
return;
}

EnsureDestinationFolderExist(destinationFile);

// reset the buffer and retrieve the content
buffer.Seek(0, SeekOrigin.Begin);
var responseBody = await reader.ReadToEndAsync();

// output the content to disk
await WriteBodyToDisk(responseBody, destinationFile);

// copy back our buffer to the response stream
buffer.Seek(0, SeekOrigin.Begin);
await buffer.CopyToAsync(responseStream);
}
finally
{
// Workaround for https://github.com/aspnet/KestrelHttpServer/issues/940
context.Response.Body = responseStream;
}

EnsureDestinationFolderExist(destinationFile);

// reset the buffer and retrieve the content
buffer.Seek(0, SeekOrigin.Begin);
var responseBody = await reader.ReadToEndAsync();

// output the content to disk
await WriteBodyToDisk(responseBody, destinationFile);

// copy back our buffer to the response stream
buffer.Seek(0, SeekOrigin.Begin);
await buffer.CopyToAsync(responseStream);
}

private void EnsureDestinationFolderExist(string destinationFile)
Expand Down

0 comments on commit 9d3caa2

Please sign in to comment.