实现System.Web.Http.WebHost.WebHostBufferPolicySelector.IHostBufferPolicySelector [英] Implementing System.Web.Http.WebHost.WebHostBufferPolicySelector.IHostBufferPolicySelector
问题描述
我想尝试关注这网博客上传大文件使用Web Api类通过Asp.Net Web窗体。如果你通过这篇文章,你会注意到,为了避免内存不足,因为缓冲的larges文件,他们建议覆盖IHostBufferPolicySelector接口。我在哪里实现接口?我在Web Api类,Global.asax或者我完全不跟踪,需要在别处做其他地方吗?
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?
推荐答案
你不需要实现这个接口,我只列出它作为参考 - 该代码已经是Web API源(在 System.Web.Http / Hosting / IHostBufferPolicySelector下的一部分。 cs
)
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
)
你需要做的是覆盖基类 System.Web.Http.WebHost.WebHostBufferPolicySelector
What you need to do is override the base class System.Web.Http.WebHost.WebHostBufferPolicySelector
这足够了:
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);
}
}
然后在 Global.asax
或 WebApiConfig.cs
(以您偏好的为准):
and then registering your new class in either Global.asax
or WebApiConfig.cs
(whichever you prefer):
GlobalConfiguration.Configuration.Services.Replace(typeof(IHostBufferPolicySelector), new NoBufferPolicySelector());
这篇关于实现System.Web.Http.WebHost.WebHostBufferPolicySelector.IHostBufferPolicySelector的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!