How to handle large file uploads via WCF?

和自甴很熟 提交于 2020-01-06 07:28:09

问题


I am looking into using WCF for a project which would require the ability for people to upload large files (64MB-1GB) to my server. How would I handle this with WCF, possibly with the ability to resume uploads.

In order to handle a larger client base, I wanted to test out JSON via WCF. How would this affect the file upload? Can it be done from JSON, or would they need to switch to REST for the upload portion?


回答1:


If you want to upload large files, you'll definitely need to look into WCF Streaming Mode.

Basically, you can change the transfer mode on your binding; by default, it's buffered, i.e. the whole message needs to be buffered on the sender, serialized, and then transmitted as a whole.

With Streaming, you can define either one-way streaming (for uploads only, for downloads only) or bidirectional streaming. This is done by setting the transferMode of your binding to StreamedRequest, StreamedResponse, or just plain Streamed.

<bindings>
   <basicHttpBinding>
      <binding name="HttpStreaming" 
               maxReceivedMessageSize="2000000"
               transferMode="StreamedRequest"/>
   </basicHttpBinding>
</bindings>

Then you need to have a service contract which either receives a parameter of type Stream (for uploads), or returns a value of type Stream (for downloads).

[ServiceContract]
public interface IFileUpload
{
    [OperationContract]
    bool UploadFile(Stream stream);
}

That should do it!




回答2:


MTOM is optimized to handle large binary data.




回答3:


You can use webHttpBinding with TransferMode streamed and a single Stream parameter or Stream response (as appropriate) for large file up/downloads, but you'd have to send any request metadata via URLs and/or headers, unless you're going to devise your own framing on the Stream. You'll have to build a custom non-HTML client (like Silverlight, Flash, etc) though, since browsers don't support random access to local files, and the normal file upload will be a form post, not JSON.



来源:https://stackoverflow.com/questions/48774474/parameter-size-limit-for-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!