When I write to the response, Katana skips sending out the Elapsed-Time response header. How can I have it set the headers for me before I write to the stream for the first time?
public override async Task Invoke(IOwinContext context)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
await Next.Invoke(context);
stopwatch.Stop();
context.Response.Headers.Add("Elapsed-Time", new[] {stopwatch.ElapsedMilliseconds.ToString()});
}
public override async Task Invoke(IOwinContext context)
{
await context.Response.WriteAsync("test");
}
After some research, the answer is that settings headers needs to happen from within OnSendingHeaders. This ensures that the headers are set before the output stream is written to. For example:
public override async Task Invoke(IOwinContext context)
{
var stopwatch = new Stopwatch();
context.Response.OnSendingHeaders(x =>
{
stopwatch.Stop();
context.Response.Headers.Add("X-Processing-Time", new[] {stopwatch.ElapsedMilliseconds.ToString()});
}, null);
stopwatch.Start();
await Next.Invoke(context);
stopwatch.Stop();
}