Implementation of System.Web.Http.WebHost.WebHostBufferPolicySelector.IHostBufferPolicySelector

I am trying to run this weblog for uploading large files using the Web Api class via Asp.Net Web Forms. If you look at the message, you will notice that in order to avoid running out of memory due to buffering large files, they recommend overriding the IHostBufferPolicySelector interface. Where can I implement the interface? Am I doing this in the Web Api class, in Global.asax, or am I completely disabled and need to implement the implementation somewhere else?

+2
asp.net-web-api
source share
1 answer

You do not need to implement this interface, I just listed it as a link - this code is already part of the web API source (under System.Web.Http/Hosting/IHostBufferPolicySelector.cs )

What you need to do is override the base class System.Web.Http.WebHost.WebHostBufferPolicySelector

It's enough:

 public class NoBufferPolicySelector : 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(), "uploading", StringComparison.InvariantCultureIgnoreCase)) return false; } return true; } public override bool UseBufferedOutputStream(HttpResponseMessage response) { return base.UseBufferedOutputStream(response); } } 

and then register your new class in Global.asax or WebApiConfig.cs (whichever you prefer):

 GlobalConfiguration.Configuration.Services.Replace(typeof(IHostBufferPolicySelector), new NoBufferPolicySelector()); 
+4
source

All Articles