WCF and streaming requests and responses
- by Cheeso
Is it correct that in WCF, I cannot have a service write to a stream that is received by the client?
My understanding is that streaming is supported in WCF for requests, responses, or both. Is it true that in all cases, the receiver of the stream must invoke Read ?
I would like to support a scenario where the receiver of the stream can Write on it. Is this supported?
Let me show it this way. The simplest example of Streaming in WCF is the service returning a FileStream to a client. This is a streamed response. The server code is like this:
[ServiceContract]
public interface IStreamService
{
[OperationContract]
Stream GetData(string fileName);
}
public class StreamService : IStreamService
{
public Stream GetData(string filename)
{
FileStream fs = new FileStream(filename, FileMode.Open)
return fs;
}
}
And the client code is like this:
StreamDemo.StreamServiceClient client =
new WcfStreamDemoClient.StreamDemo.StreamServiceClient();
Stream str = client.GetData(@"c:\path\to\myfile.dat");
do {
b = str.ReadByte(); //read next byte from stream
...
} while (b != -1);
(example taken from http://blog.joachim.at/?p=33)
Clear, right? The server returns the Stream to the client, and the client invokes Read on it.
Is it possible for the client to provide a Stream, and the server to invoke Write on it?
In other words, rather than a pull model - where the client pulls data from the server - it is a push model, where the client provides the "sink" stream and the server writes into it.
Is this possible in WCF, and if so, how? What are the config settings required for the binding, interface, etc?
The analogy is the Response.OutputStream from an ASP.NET request. In ASPNET, any page can invoke Write on the output stream, and the content is received by the client. Can I do something similar in WCF?
Thanks.