Search code examples
c#remotingarraysbytechunking

How to return a byte[] doing chunking using remoting over http in C#?


In .Net remoting over http, we return a byte[] back to the client by doing a SerilizationInfo.AddValue(SerializationInfoValueName, ((MemoryStream)writer.BaseStream).GetBuffer(), typeof(byte[])) call.

However, sometimes the byte[] size gets too big and causes an OutOfMemory exception. The only remedy seems to be utilize some form of chunking. A move to WCF seems most logical, however that's not possible in the near future.

So any suggestions on how to implement a generic chunking solution?


Solution

  • A more secure way to do it is to just remote a stream on the object you're returning from the server. Stream extends MarshalByRefObject, so if you include a reference to an open Stream (eg, pointing at the file data or buffer you want to send) in an object that's sent to the client, the client will call back to the server when you call methods on the stream proxy. This way, the client can use whatever buffer sizes it wants (within reason) to move the data around.