的WebAPI请求流媒体支持 [英] WebAPI Request Streaming support

查看:341
本文介绍了的WebAPI请求流媒体支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写信,要求我接受一个文件上传和转发到另一个HTTP端点文件的ASP.NET Web API应用程序。

I am writing an ASP.NET Web API application that requires me to accept a file upload and forward that file on to another HTTP endpoint.

我担心,如果许多用户尝试上载的每个一个100MB的文件(这是一个有效的用例),那么我的应用程序将有一个大的内存空间,并根据大的请求数量这个脚印可能增长到大,我的应用程序将倒下了,死亡。

I am concerned that if many users try to upload a 100MB file each (which is a valid use case) then my application will have a large memory footprint and depending on the volume of large requests this footprint could grow to large and my application would keel over and die.

理想情况下,我想立即开始在Web服务器启动流文件到其他HTTP端点接收该文件以显著降低服务器上的负载。

Ideally I'd like begin streaming the file to the other HTTP end point as soon as the web server starts to receive the file to significantly reduce the load on the server.

我敢肯定,这个过程中有一个名字,但我不知道它 - 它正在寻找它而艰难的。

I'm sure this process has a name but I don't know it - which is making searching for it rather tough.

我已经做了相当多的工作与响应流在Web API中,但我以前从来没有考虑请求流。

I've done quite a bit of work with Response Streaming in the Web API but I've never had to consider request streaming before.

最好的,我可以告诉我需要工作,如何:

Best I can tell I need to work out how to:


  • 开始处理数据流,它已经完成,然后再上传。

  • 使用HttpClient的流相同的数据流给其他HTTP终点同样的要求。

任何人都可以提供我一些指点?

Can anyone offer me some pointers?

推荐答案

这是一个有趣的问题。我会试着尽我所能,给一些普通的指针。

That's an interesting question. I'll try to do my best to give some general pointers.

几件事情要考虑:

1)网络API默认缓冲区请求,以便您担心内存占用量可能是相当肯定是有道理的。您可以强制的Web API在流模式要求的工作:

1) Web API by default buffers requests so your fear that the memory footprint might be considerable is definitely justified. You can force Web API to work with requests in a streamed mode:

    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);
       }
    }

然后更换服务:

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

请注意,由于在这一点上虚拟主机提供商和SelfHost之间的差异,这种变化在虚拟主机提供商才是可能的。如果您的端点selfHosted,你就必须在GlobalConfig级别设置流模式:

Please note that due to differences between WebHost and SelfHost at this point, such change is only possible in WebHost. If your endpoint is selfHosted, you would have to set the streaming mode at the GlobalConfig level:

//requests only
selfHostConf.TransferMode = TransferMode.StreamedRequest;
//responses only
selfHostConf.TransferMode = TransferMode.StreamedResponse;
//both
selfHostConf.TransferMode = TransferMode.Streamed;

我的博客上讲述之前更加详细地大文件在处理网络API - <一个href=\"http://www.strathweb.com/2012/09/dealing-with-large-files-in-asp-net-web-api/\">http://www.strathweb.com/2012/09/dealing-with-large-files-in-asp-net-web-api/所以希望你会发现有用的。

I have blogged about dealing with large files in Web API in more details before - http://www.strathweb.com/2012/09/dealing-with-large-files-in-asp-net-web-api/ so hopefully you'll find that useful.

2)其次,如果你使用的HttpClient ,在.NET 4中默认缓存请求的身体,所以你应该使用。 4.5的NEt。

2) Secondly, if you use HttpClient, in .NET 4 it buffers the requests body by default, so you should really use .NEt 4.5.

如果你必须使用.NET 4,你有 HttWebRequest 工作直接:
  - <一个href=\"http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.allowreadstreambuffering.aspx\">http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.allowreadstreambuffering.aspx
  - <一个href=\"http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.allowwritestreambuffering.aspx\">http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.allowwritestreambuffering.aspx

If you have to use .NET 4 you have to work with HttWebRequest directly: - http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.allowreadstreambuffering.aspx - http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.allowwritestreambuffering.aspx

3)至于数据推动,如果你想这样做,这是绝对有可能,有 PushStreamContent 客户端。
亨里克有一个简短的介绍后在这里 - <一个href=\"http://blogs.msdn.com/b/henrikn/archive/2012/04/23/using-cookies-with-asp-net-web-api.aspx\">http://blogs.msdn.com/b/henrikn/archive/2012/04/23/using-cookies-with-asp-net-web-api.aspx (它是基于Web的API RC位,所以你可能需要调整一些签名等)
我也博客上讲述这里推流数据块 - <一个href=\"http://www.strathweb.com/2013/01/asynchronously-streaming-video-with-asp-net-web-api/\">http://www.strathweb.com/2013/01/asynchronously-streaming-video-with-asp-net-web-api/

3) As far as pushing the data to the client that's definitely possible if you want to do that, with PushStreamContent. Henrik has a short introductory post here - http://blogs.msdn.com/b/henrikn/archive/2012/04/23/using-cookies-with-asp-net-web-api.aspx (it's based on Web API RC bits so you might need to adjust some signatures etc.) I also blogged about pushing chunks of stream data here - http://www.strathweb.com/2013/01/asynchronously-streaming-video-with-asp-net-web-api/

编辑::要看到一个例子,如果 PushStreamContent 的要求,你可以看看这个样品溶液 - <一个href=\"http://aspnet.$c$cplex.com/SourceControl/changeset/view/bb167f0b0013#Samples/Net45/CS/WebApi/UploadXDocumentSample/ReadMe.txt\">http://aspnet.$c$cplex.com/SourceControl/changeset/view/bb167f0b0013#Samples/Net45/CS/WebApi/UploadXDocumentSample/ReadMe.txt

To see an example if PushStreamContent in the request, you can have a look at this sample solution - http://aspnet.codeplex.com/SourceControl/changeset/view/bb167f0b0013#Samples/Net45/CS/WebApi/UploadXDocumentSample/ReadMe.txt

这篇关于的WebAPI请求流媒体支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆