Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

ASP.NET Web API Controller output is always buffered

$
0
0

How to send output without buffering? I defined my API controller this 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))
                {
                    for (int i = 0; i < 5; i++)
                    {
                        writer.WriteLine("Eh?");
                        writer.Flush();
                        Thread.Sleep(2000);
                    }
                }
            },"text/plain");

        return response;
    }
}

Output appears in the browser all at once, so it looks like it waits start sending it till completion. I defined this attribute:

[AttributeUsage(AttributeTargets.Class,AllowMultiple=false)]classNoBufferAttribute:Attribute,IControllerConfiguration{publicvoidInitialize(HttpControllerSettings controllerSettings,HttpControllerDescriptor controllerDescriptor){
        controllerSettings.Services.Replace(typeof(IHostBufferPolicySelector),newBufferPolicy());}classBufferPolicy:IHostBufferPolicySelector{public bool UseBufferedInputStream(object hostContext){returnfalse;}public bool UseBufferedOutputStream(HttpResponseMessage response){returnfalse;}}}

And applied it to controller:

[NoBuffer]publicclassDefaultController:ApiController{...}

It did not help. All the output appears in the browser the same time.


Viewing all articles
Browse latest Browse all 4850

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>