Question

Using a WCF service to receive a stream of data (inbound from the client) that can be very, very large, what is the most efficient way to perform two operations on the stream "at once"? I realize the question is broad. Examples of the type of operation might include

but the key abstract point is that both operations require some kind of read operation, and the stream is not seekable (which, as I understand it, means I have to copy the stream if the operations are performed sequentially).

EDIT: This answer seems relevant too.

Was it helpful?

Solution

Read a buffer at a time and pass it to the two consumers. There's nothing in your question that would prevent this simple solution from being used. That would look like this:

while(dataAvailable) {
 var buffer = Read();
 Write1(buffer);
 Write2(buffer);
}

And a practical example.

You can also play with wrapper streams that perform a side-effect (such as hashing) and just pass on the buffer to the next stream.

It becomes more complicated if you have multiple pieces of code requiring reads from a stream (such as two independent XmlReader's). In that case you need to demultiplex. You probably need to keep a buffer of data and only when all consumers have read that buffer you load the next buffer. This would involve threading and synchronization because multiple independent readers need to read in lock-step.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top