4

I am trying to following this web blog on uploading large files using the Web Api class via Asp.Net Web Forms. If you look through the post you will notice that in order to avoid an out of memory because of buffering of larges files, they recommend overriding the IHostBufferPolicySelector interface. Where do I implement the interface? Do I do it in the Web Api class, in the Global.asax or am I completely off track and need to do the implementation somewhere else?

1 Answer 1

5

You don't need to implement this interface, I only listed it as a reference - that code is already part of 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

This is 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 registering your new class in either Global.asax or WebApiConfig.cs (whichever you prefer):

GlobalConfiguration.Configuration.Services.Replace(typeof(IHostBufferPolicySelector), new NoBufferPolicySelector());
1
  • Thanks! I did this earlier but I was trying to go both routes.
    – DaNet
    Oct 3, 2012 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.