title: WCF and streaming requests and responses
body: <p>Is it correct that in WCF, I cannot have a service <em>write</em> to a stream that is received by the client? </p>
  
  <p>Streaming is supported in WCF for requests, responses, or both. 
  I would like to support a scenario where the data generator (either the client in case of streamed request, or the server in case of streamed response) can Write on the stream.  Is this supported?  </p>
  
  <p>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 a WCF service - invoke Write on a stream that is received by the client?  </p>
  
  <p>Let me explain with a WCF illustration. The simplest example of Streaming in WCF is the service returning a FileStream to a client. This is a streamed response. The server code to implement this, is like this: </p>
  
  <pre><code>[ServiceContract]
  public interface IStreamService
  {
      [OperationContract]
      Stream GetData(string fileName);
  }
  public class StreamService : IStreamService
  {
      public Stream GetData(string filename)
      {
          return new FileStream(filename, FileMode.Open)
      }
  }
  </code></pre>
  
  <p>And the client code is like this: </p>
  
  <pre><code>StreamDemo.StreamServiceClient client = 
      new WcfStreamDemoClient.StreamDemo.StreamServiceClient();
  Stream str = client.GetData(@"c:\path\on\server\myfile.dat");
  do {
    b = str.ReadByte(); //read next byte from stream
    ...
  } while (b != -1);
  </code></pre>
  
  <p>(example taken from <a href="http://blog.joachim.at/?p=33" rel="noreferrer">http://blog.joachim.at/?p=33</a>) </p>
  
  <p>Clear, right?  The server returns the Stream to the client, and the client invokes Read on it. </p>
  
  <p>Is it possible for the client to provide a Stream, and the server to invoke Write on it?<br>
  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.  The server-side code might look similar to this: </p>
  
  <pre><code>[ServiceContract]
  public interface IStreamWriterService
  {
      [OperationContract]
      void SendData(Stream clientProvidedWritableStream);
  }
  public class DataService : IStreamWriterService
  {
      public void GetData(Stream s)
      {
          do {
            byte[] chunk = GetNextChunk();
            s.Write(chunk,0, chunk.Length);
          } while (chunk.Length &gt; 0); 
      }
  }
  </code></pre>
  
  <p>Is this possible in WCF, and if so, how?  What are the config settings required for the binding, interface, etc?  What is the terminology?</p>
  
  <p>Maybe it will just work?  (I haven't tried it)</p>
  
  <p>Thanks.</p>