I've been trying to cobble up some middleware that will allow me to measure the processing time on a request. This example gave me a good starting point, but I've run into trouble.
In the code below, I am able to measure the process time and insert it in a div (using HTML Agility Pack). However, the original contents of the page get duplicated. I think I'm doing something incorrectly with the context.Response.Body property in UpdateHtml(), but cannot figure out what it is. (I made some comments in the code.) If you see anything that looks incorrect, could you please let me know?
Thanks.
public class ResponseMeasurementMiddleware
{
private readonly RequestDelegate _next;
public ResponseMeasurementMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
var watch = new Stopwatch();
watch.Start();
context.Response.OnStarting(async () =>
{
var responseTime = watch.ElapsedMilliseconds;
var newContent = string.Empty;
var existingBody = context.Response.Body;
string updatedHtml = await UpdateHtml(responseTime, context);
await context.Response.WriteAsync(updatedHtml);
});
await _next.Invoke(context);
}
private async Task<string> UpdateHtml(long responseTime, HttpContext context)
{
var newContent = string.Empty;
var existingBody = context.Response.Body;
string updatedHtml = "";
//I think I'm doing something incorrectly in this using...
using (var newBody = new MemoryStream())
{
context.Response.Body = newBody;
await _next(context);
context.Response.Body = existingBody;
newBody.Position = 0;
newContent = await new StreamReader(newBody).ReadToEndAsync();
updatedHtml = CreateDataNode(newContent, responseTime);
}
return updatedHtml;
}
private string CreateDataNode(string originalHtml, long responseTime)
{
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(originalHtml);
HtmlNode testNode = HtmlNode.CreateNode($"<div><h2>Inserted using Html Agility Pack: Response Time: {responseTime.ToString()} ms.</h2><div>");
var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");
htmlBody.InsertBefore(testNode, htmlBody.FirstChild);
string rawHtml = htmlDoc.DocumentNode.OuterHtml; //using this results in a page that displays my inserted HTML correctly, but duplicates the original page content.
//rawHtml = "some text"; uncommenting this results in a page with the correct format: this text, followed by the original contents of the page
return rawHtml;
}
}