Large Messages and WCF

CLR typed objects (and buffered messages) are great for a large breadth of applications. Sometimes though, you just want to push bytes. WCF provides a convenient way of doing so with “streamed methods”.

To use WCF’s “Streaming API” you write an operation with an input and/or output of type System.IO.Stream. Because the Stream parameter utilizes the entire body, if an input or an output (i.e. ref parameter, out parameter, or return value) is of type Stream then it must be the only input/output specified in your OperationContract.

When using a Streaming method, you need to pass in a Stream object that provides data incrementally from its Read() method. One common area of confusion is that your return Stream must also be written with the Read() method providing the data. The mental model is that the client is “reading” the return value, and not that the server is “writing” the result.

Now that your operation has been architected so that it no longer requires buffering, you have to choose a binding that is compatible with moving a lot of bytes around. Yasser wrote a nice post on this topic here. In a nutshell you need to:

  • Pick an encoding (Binary for Indigo< ->Indigo scenarios or MTOM if you require interop)
  • Enable streaming on your binding (either through TransferMode.Streamed on a transport or by using chunking at the top of your binding stack)

3 thoughts on “Large Messages and WCF

  1. Pingback: : .NET Resources

  2. Pingback: » Blog Archive » Security and Streamed Messages

  3. Pingback: New and Notable 92 - Sam Gentile

Leave a Reply

Your email address will not be published. Required fields are marked *