Skip to content

Commit

Permalink
Merge pull request #1 from benaadams/patch-1
Browse files Browse the repository at this point in the history
Kestrel bug workaround
  • Loading branch information
MaximRouiller authored Jul 6, 2016
2 parents 99ea5f4 + 9d3caa2 commit e9dd05d
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 e9dd05d

Please sign in to comment.