Web API2 WebHostBufferPolicySelector UseBufferedInputStream override RRS feed

  • Question

  • User724214976 posted


    I would like to ask if it is necessary to override  UseBufferedInputStream  method in Custom WebHostBufferPolicySelector, where base on rout i can turon on/off buffering.

    What i mean is that I am uploading large file through API and I want to use streaming, so I wont use that much of memory, I heard that by default WEB API is buffering request and i want to avoid it, so according to some materials What i have done is:

     public class NoBufferWebHostPolicySelector : WebHostBufferPolicySelector
            public override bool UseBufferedInputStream(object hostContext)
                var context = hostContext as HttpContextBase;
                if (context != null)
                    if (string.Equals(context.Request.RequestContext.RouteData.Values["controller"].ToString(), "upload", StringComparison.InvariantCultureIgnoreCase))
                        return false;
                return true;
            public override bool UseBufferedOutputStream(HttpResponseMessage response)
                return base.UseBufferedOutputStream(response);
     protected void Application_Start()
                GlobalConfiguration.Configuration.Services.Replace(typeof(IHostBufferPolicySelector), new NoBufferWebHostPolicySelector());

    But then UseBufferedInputStream  method is called, context.Request.RequestContext.RouteData is always empty.

    So now i am wondering if this is really thing I need to do, and if so, why I have context.Request.RequestContext.RouteData empty in this method.

    Anyone can help me?

    Wednesday, November 12, 2014 6:20 AM

All replies