title: Transferring large payloads of data (Serialized Objects) using wsHttp in WCF with message security
body: <p>I have a case where I need to transfer large amounts of serialized object graphs (via <a href="http://msdn.microsoft.com/en-us/library/system.runtime.serialization.netdatacontractserializer.aspx" rel="noreferrer">NetDataContractSerializer</a>) using WCF using wsHttp. I'm using message security and would like to continue to do so. Using this setup I would like to transfer serialized object graph which can sometimes approach around 300MB or so but when I try to do so I've started seeing a exception of type System.InsufficientMemoryException appear. </p>
  
  <p>After a little research it appears that by default in WCF that a result to a service call is contained within a single message by default which contains the serialized data and this data is buffered by default on the server until the whole message is completely written. Thus the memory exception is being caused by the fact that the server is running out of memory resources that it is allowed to allocate because that buffer is full. The two main recommendations that I've come across are to use streaming or chunking to solve this problem however it is not clear to me what that involves and whether either solution is possible with my current setup (wsHttp/NetDataContractSerializer/Message Security). So far I understand that to use streaming message security would not work because message encryption and decryption need to work on the whole set of data and not a partial message. Chunking however sounds like it might be possible however it is not clear to me how it would be done with the other constraints that I've listed. If anybody could offer some guidance on what solutions are available and how to go about implementing it I would greatly appreciate it.</p>
  
  <p>I should add that in my case I'm really not worried about interoperability with other clients as we own and control each side of the communication and use the shared interface pattern for data transfered to either side. So I'm open to any idea that fits inside of the constraints of using wsHttp with message security to transfer object graphs serialized using NetDataContractSerializer and I prefer a solution where I don't have to change my existing services and surrounding infrastructure drastically.</p>
  
  <p>Related resources:</p>
  
  <ul>
  <li><a href="http://msdn.microsoft.com/en-us/library/aa717050.aspx" rel="noreferrer">Chunking Channel</a></li>
  <li><a href="http://msdn.microsoft.com/en-us/library/ms789010.aspx" rel="noreferrer">How to: Enable Streaming</a></li>
  <li><a href="http://nirajrules.wordpress.com/2009/08/03/mtom-vs-streaming-vs-compression-%E2%80%93-large-attachments-over-wcf/" rel="noreferrer">Large attachments over WCF</a></li>
  <li><a href="http://msdn.microsoft.com/en-us/library/ms751458.aspx" rel="noreferrer">Custom Message Encoder</a></li>
  <li><a href="http://sivapinnaka.spaces.live.com/Blog/cns!B027EF7E7070AD69!195.entry" rel="noreferrer">Another spotting of InsufficientMemoryException</a></li>
  <li><a href="http://social.msdn.microsoft.com/forums/en-US/wcf/thread/3724bcb8-dc04-47d4-a5fa-e89403b9e38a" rel="noreferrer">Non-Duplex Chunking Channel needed</a></li>
  <li><a href="http://weblogs.asp.net/cibrax/archive/2008/06/10/streaming-large-content-with-wcf-and-deferred-execution.aspx" rel="noreferrer">Streaming large content with WCF and deferred execution</a></li>
  </ul>
  
  <p>I'm also interested in any type of compression that could be done on this data but it looks like I would probably be best off doing this at the transport level once I can transition into .NET 4.0 so that the client will automatically support the gzip headers if I understand this properly.</p>
  
  <h2>Update (2010-06-29):</h2>
  
  <p>Some history on how I derived at the conclusion that the buffered message being too large was causing my problem. Originally I saw the <a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.communicationexception.aspx" rel="noreferrer">CommunicationException</a> below while testing.</p>
  
  <p><em>The underlying connection was closed: The connection was closed unexpectedly.</em></p>
  
  <p>Eventually after running this and doing some more logging I found the underlying <a href="http://msdn.microsoft.com/en-us/library/system.insufficientmemoryexception.aspx" rel="noreferrer">InsufficientMemoryException</a> exception that was causing the problem with the specified message.</p>
  
  <p><em>Failed to allocate a managed memory buffer of 268435456 bytes. The amount of available memory may be low.</em></p>
  
  <p>Which originated from the following method.</p>
  
  <p><em>System.ServiceModel.Diagnostics.Utility.AllocateByteArray(Int32 size)</em></p>
  
  <p>So in otherwords the failure came from allocating the array. When writing the same data serialized to disk it takes up around 146MB and if I cut it by half then I stop getting the error however I haven't dug much more into the specific threshold that breaks my buffer and whether it specific to my system or not.</p>
  
  <h2>Update (2010-12-06):</h2>
  
  <p>I guess at this point I'm looking for some clarifcation for the following. My understanding is that by default with WCF wsHttp with message security that a whole message (generally the whole set of data I'm returning) needs to be buffered on the server before the response is sent back to the client and thus causing my problems.</p>
  
  <p>Possible solutions:</p>
  
  <ul>
  <li>Constraining data size - By using some form of compression, encoding, or limiting of actual data returned by using some sort of paging like method in order to avoid consuming the maximum capacity of the outgoing buffer.</li>
  <li>Streaming - Allows large amounts of data to be sent through WCF in a streaming fashion however this is not compatible with wsHttp or MessageSecurity since these techniques require buffering all the data.</li>
  <li>Chunking Channel - Allows data to be broken up into separate messages but at this point I'm not sure of the constraints this has on service contract design and whether I can still use wsHttp with message binding.</li>
  </ul>
  
  <p>Limiting the data that I can return only works up to a point and as with the Streaming option these options require coding a lot of the lower level work outside of the WCF service calls. So I guess what I need to know is whether any possible implementation of the chunking channel can side-step the large message issues by allowing a single set of data to be broken up into separate messages on the server and then pieced together on the client in such a way that I do not have to change the interface/shape of existing service contracts and in a way that the process is pretty much hidden from the client and server portion of each service implementation while still using message security and wsHttp. If the chunking-channel is going to require me to re-write my service contracts to expose streams then I don't see how this is really any different than the Streaming solution. If somebody can simply answer these questions for me I'll award them with the bounty and mark it as the answer.</p>