Tuesday, December 25, 2007

WCF Message Size Tester

Recently on the WCF Forum someone suggested that WCF wasn't able to send messages larger than 2 GB, so I wrote up a little example to disprove this theory.  I was able to send a 500 GB message with no problems, and I'm guessing that if I let it run long enough, there will be no problem sending up to 8 million terabytes, the maximum size of a WCF message (Int64.MaxValue).

The main "trick" to sending messages larger than 2 GB is to change the binding's TransferMode from its default of TransferMode.Buffered to TransferMode.Streamed.  Besides enabling you to send messages with minimal in-memory overhead, this also allows you to set MaxReceivedMessageSize larger than 2 GB without WCF throwing an exception when it tries to build the channel stack.

It's worth noting that MaxReceivedMessageSize is enforced by WCF regardless of whether the TransferMode is Buffered or Streamed.  The same is true for SendTimeout and ReceiveTimeout, so these still need to be adjusted accordingly.

Stream-based messaging in WCF is typically accomplished by using service operations that take a Stream as the only parameter, return a Stream, or both.  So what kind of Stream should I ask WCF to send for me?  I wanted my example to run as fast as possible, which means I didn't want it to be touching the disk in order to send or receive messages.  And I don't have enough memory or address space to buffer a 500 GB MemoryStream.  So I created a little hack stream implementation whose Read method leaves the byte buffer untouched and simply returns the appropriate number of bytes "read".  This results in sending 500 GB of zeros. :-)

Download the example code here.


Karl Shifflett said...


Greetings and Happy New Year!

Have you tried using Mole v4 with your WCF development yet?

Team Mole would be very interested in your feedback from a WCF perspective.

We have included a very simple WCF project in the downloads and I have tested it. I like the way you can drill all around the WCF classes and can "see" in context how it all fits together.

I can add any tips you may have to the Code Project article and my blog on Mole v4.

Best to you and thank you for your blog. I'm subscribed!



Oran Dennison said...

Hi Karl, I've been following Mole (and your and Josh's blogs) from the beginning of Woodstock/Mole and have even downloaded several versions along the way, but haven't checked out the latest yet. I must say, I haven't seen a project go through so many major releases so fast! You guys are unstoppable. What next... a WPF-based Visual Studio replacement? ;-) Who knows, maybe that's part of what Microsoft's up to...

Anonymous said...

Thanks Oran

Would you please write the whole WCF project of Streaming Large Data?

A service which retrieve data from DB and populate it to Excel (e.g)

I m really beginner in WCF. Is it big?

so much thanks.

Hamid dua puluh satu said...

i can't get the code