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?
Middleware #1
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()});
}
Middleware #2
public override async Task Invoke(IOwinContext context)
{
await context.Response.WriteAsync("test");
}
Use
Response
'sHeaders
property.UPDATE
Your Middlewares looks correct. Perhaps, you have issue in the configuration.
Check, if you have pipelined your middleawres like this:
By the way, you haven't necessity of two midlewares. This will work as wall:
and configuration is:
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: