I want to add a processing time middleware to my ASP.NET Core WebApi like this
public class ProcessingTimeMiddleware
{
private readonly RequestDelegate
Using an overload of OnStarting method:
public async Task Invoke(HttpContext context)
{
var watch = new Stopwatch();
context.Response.OnStarting(() =>
{
watch.Stop();
context
.Response
.Headers
.Add("X-Processing-Time-Milliseconds",
new[] { watch.ElapsedMilliseconds.ToString() });
return Task.CompletedTask;
});
watch.Start();
await _next(context);
}