I'm writing Owin middleware for a very simple self-hosted endpoint using WebApp.Start();
To write to the response stream I have code of the form:
var writer = new StreamWriter(context.Response.Body, Encoding.UTF8);
serialiser.Serialize(writer, output);
writer.Flush();
Should I dispose either the writer or the context.Response.Body
? Normally I have using
statements all over the place, but I didn't instantiate the response stream and I have a vague recollection that there's a convention that disposal is the responsibility of the component that instantiated the IDisposable.
No, the server owns those streams and will clean them up at the end of the request.
http://owin.org/html/spec/owin-1.0.html
3.4 "The application SHOULD NOT close or dispose the given stream unless it has completely consumed the request body. The stream owner (e.g. the server or middleware) MUST do any necessary cleanup once the application delegate's Task completes."
3.5 "The application SHOULD NOT close or dispose the given stream as middleware may append additional data. The stream owner (e.g. the server or middleware) MUST perform any necessary cleanup once the application delegate's Task completes."