c# - ASP.NET Web API Controller output is always buffered -
how send output without buffering? defined api controller way:
public class defaultcontroller : apicontroller { [httpget] [route] public httpresponsemessage get() { var response = request.createresponse(); response.content = new pushstreamcontent( (output, content, context) => { using (var writer = new streamwriter(output)) { (int = 0; < 5; i++) { writer.writeline("eh?"); writer.flush(); thread.sleep(2000); } } }, "text/plain"); return response; } }
output appears in browser @ once, looks waits start sending till completion. defined attribute:
[attributeusage(attributetargets.class, allowmultiple = false)] class nobufferattribute : attribute, icontrollerconfiguration { public void initialize(httpcontrollersettings controllersettings, httpcontrollerdescriptor controllerdescriptor) { controllersettings.services.replace( typeof(ihostbufferpolicyselector), new bufferpolicy()); } class bufferpolicy : ihostbufferpolicyselector { public bool usebufferedinputstream(object hostcontext) { return false; } public bool usebufferedoutputstream(httpresponsemessage response) { return false; } } }
and applied controller:
[nobuffer] public class defaultcontroller : apicontroller { ... }
it did not help. output appears in browser same time.
update
it looks problem flushing. changed code following:
var response = request.createresponse(); response.content = new pushstreamcontent( (output, content, context) => { using (var writer = new streamwriter(output)) { var s = stopwatch.startnew(); while (s.elapsed < timespan.fromseconds(10)) { writer.writeline(s.elapsed); writer.flush(); } } }, "text/plain");
now can see output in progress. disabling gzip encoding not see content in smaller chunks.
Comments
Post a Comment