Wednesday, August 29, 2007 10:42 AM cibrax

Sending Attachments with WCF

WCF mainly supports two modes to send attachments in a efficient way, an streamed mode and a buffered/chunked mode.

In the first mode, the transport basically transfers large amount of data using small buffers with a minimal overhead. This mode of transferring the data has some drawbacks, for instance it does not support message security (Only transport security through SSL), or the robustness of reliable messaging. On the other hand, interoperability is easier to archive since no WS-* standard is used. Nicholas describes the restrictions very well on this post, http://blogs.msdn.com/drnick/archive/2006/03/31/565558.aspx

I will show a simple example that uses the Streamed mode to upload a file to the server. The contract for this service looks as follow:

[ServiceContract()]

public interface IFileTransferService

{

  [OperationContract(IsOneWay = true)]

  void Upload(FileTransferRequest request);

}

As Nicholas mentioned on his post, when this mode is used, only the stream object can go in the message body, the rest of the arguments must go as headers. That is the main reason of using a message contract FileTransferRequest.

[MessageContract()]

public class FileTransferRequest

{

  [MessageHeader(MustUnderstand = true)]

  public string FileName;

 

  [MessageBodyMember(Order = 1)]

  public System.IO.Stream Data;

 

}

Processing large files with buffered transfer does not scale well, and could be something impossible to do because of the high memory requeriments on the client and server side. The workaround for this is chunking combined with reliable messaging, which basically split large files in small fragments (For instance, 64 o 128 KB fragments) and use reliable messaging to assure the ordered delivery of those fragments. WCF does not provide a transport with chunk support by default, but fortunately it is implemented in an example of the SDK.

This simple is under the folder [SDK folder]\Samples\Extensibility\Channels\ChunkingChannel

In addition to the mode used to transfer the streams, WCF also supports the concept of message encoders, which basically serializes a xml infoset into a stream of bytes using an specific encoding. WCF ships by default with three encoders, Text, Binary and MTOM. Any of the features provided by these encoders should be considered carefully to avoid performance issues in the application.

The Text and MTOM encoders provides interoperability with other platforms while the Binary formatter uses a proprietary format. Anyway, let's discuss these types of encoder more in detail:

Text Encoder: As it name says, it is a text message encoder and supports both plain XML encoding (For Rest/POST scenarios) as well as SOAP Encoding. When this encoder is used, the attachments are encoded as base64 and added as part of the xml message. This transformation to a base64 encoding makes the final size of the attachments bigger, almost 30% bigger than the original size.

Binary Encoder: It uses a compact binary format, optimized for a WCF to WCF communication. This encoder is the recommend one for sending attachments when interoperability is not a requirement.

MTOM Encoder:  It uses a Message Transmission Optimization Mechanism (MTOM) encoding. MTOM is a efficient technology for transmitting large blocks of binary data in a soap message as-is, without conversion to text. In addition, MTOM is a interoperable standard and also supports Message Security (WS-Security). When Message Security is used, WCF basically performs the following steps:

1. Encodes all the blocks of binary data as Base64.

2. Applies message security (Integrity and Confidentiality) to the resulting block of Base64 text.

3. Adds the security headers to the soap message

4. Transmits the binary data separately from the soap message (The Base64 data created in the step 2 is discarded)

For more details about moving large amounts of data and final recommendations, see this excellent post of Yasser Shohoud.

Examples,

Buffered Upload

Streamed Upload

Filed under: ,

Comments

# New and Notable 185

Wednesday, August 29, 2007 3:00 PM by Sam Gentile

WCF Sending Attachments with WCF BizTalk Server/XML/ESB Marty announces CTP3 of ESB Guidance How the

# Sending Attachments with WCF « Tuff Stuff

Wednesday, August 29, 2007 7:21 PM by Sending Attachments with WCF « Tuff Stuff

Pingback from  Sending Attachments with WCF « Tuff Stuff

# Sending Attachments with WCF « vincenthome’s Software Development

Pingback from  Sending Attachments with WCF « vincenthome’s Software Development

# New and Notable 185

Wednesday, September 12, 2007 6:50 PM by Sam Gentile Personal Blog

WCF Sending Attachments with WCF BizTalk Server/XML/ESB Marty announces CTP3 of ESB Guidance How the

# re: Sending Attachments with WCF

Tuesday, October 23, 2007 2:52 PM by Perry

Hi,

Your example BufferedUpload.zip actually contains the StreamedUpload solution. Can you update it please? I'm interested in viewing and comparing the differences.

# re: Sending Attachments with WCF

Wednesday, June 18, 2008 12:24 PM by Mayank

Really cool Blog Thanks.

# re: Sending Attachments with WCF

Tuesday, July 01, 2008 10:23 AM by Roman S. Golubin

Thanks!

This is a usefull article about WCF Streamed mode!

# re: Sending Attachments with WCF

Saturday, November 22, 2008 11:49 PM by Bharat

You are a lifesaver. Thank you for this.

# re: Sending Attachments with WCF

Thursday, January 15, 2009 4:03 PM by Mark

Thanks for the useful post, I used it to get straming uploads hosted on IIS over http and https. If you or anyone else is interested I have posted some notes and code on the following link.

mark-csharp.blogspot.com/.../wcf-file-transfer-streaming-chunking.html

../mark

# re: Sending Attachments with WCF

Friday, January 30, 2009 10:59 AM by Ram

As someone noted earlier, both of the downloads are for projects named StreamedUpload, and both of the app.config files specify Streamed as the transfer mode. While the example of a streamed application is useful in itself, it doesn't really help in comparing streamed mode to buffered mode.

# WCF------Sending Attachments with WCF

Wednesday, April 01, 2009 4:34 AM by 鸡尾虾的壳

WCFmainlysupportstwo

Leave a Comment

(required) 
(required) 
(optional)
(required)