Wcf Download Large File

Posted By admin On 12/01/22
Active8 months ago

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.

Now, if we run the above code, it will work fine and file will be successfully downloaded. Cheers Although, we got the answer to questions asked at the start of this WCF Service tutorial but this approach has some disadvantages with respect to performance.

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?

11.7k29 gold badges104 silver badges169 bronze badges

3 Answers

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.

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).

That should do it!

606k137 gold badges1160 silver badges1292 bronze badges
Darin DimitrovDarin Dimitrov
879k233 gold badges3076 silver badges2792 bronze badges

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.

12.3k1 gold badge27 silver badges36 bronze badges

Not the answer you're looking for? Browse other questions tagged c#wcfjsonlarge-file-upload or ask your own question.

Active7 years, 6 months ago

I have quite some trouble creating a WCF service that supports downloads of very large files. I have read a lot of guides on how to set transferMode attribute to Streamed, increase all messageSize and bufferSize attributes to Int32.MaxValue, and still I have no luck. (I am also returning the stream as the message body via the MessageBodyMember attribute, and metadata is sent via the headers using the MessageHeader attributes).

Cfg download

If I set all these attributes, I can download smaller files fine, but when I try to download 1-2GB files I simply get a 400 bad request error which makes it rather hard to debug...

My service should ideally support file sizes of at least 8GB. Is this even doable with WCF? The various messageSize attributes of the web.config file seem to be limited to Int32.MaxValue which equals a maximum file size of 2GB.

From my studies I have found that it seems I will have to use WebClient.DownloadFile instead.Files should only be available for download to users who have the required rights. With WCF my download method could take a token-parameter that the server could check and return the stream only if the user had rights to download the requested file. This does not seem straight forward using the WebClient approach. If anyone has some guidelines on how to do this (via the WebClient), I would very much appreciate it.

Ideally my WCF service should administer and provide user tokens and somehow bind to every individual file what tokens are currently legal (tokens should be usable only once). Download should then happen via the WebClient.

Thanks in advance for any clues.

Janus VarmarkenJanus Varmarken
1,7383 gold badges14 silver badges34 bronze badges

Cfg Download


Wcf File Streaming

1 Answer

You can do this in WCF. Many moons ago I built a service that did this (we didn't have a web server as part of our configuration). We used WCF streaming:

The strategy to deal with large payloads is streaming. While messages, especially those expressed in XML, are commonly thought of as being relatively compact data packages, a message might be multiple gigabytes in size and resemble a continuous data stream more than a data package. When data is transferred in streaming mode instead of buffered mode, the sender makes the contents of the message body available to the recipient in the form of a stream and the message infrastructure continuously forwards the data from sender to receiver as it becomes available.


Return File Wcf

13.3k1 gold badge24 silver badges46 bronze badges

Wcf Download Large File

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