title: WCF Client is Buffering with Streamed Transfer Mode
body: <p>My end goal is to enable upload of large video files and streams to a WCF service which is configured for Streamed transfer mode. The problem I have is that my WCF client is only issuing a request to the service after the stream had been read to the end.</p>
  
  <p>A primary use case would be relaying live streams to the service which have no predefined ending point, so it is not practical to fully read these streams before sending any data. To test this functionality, I have created an 'infinite' stream:</p>
  
  <pre><code>public class InfiniteStream : Stream
  {
      private Random _random;
  
      public InfiniteStream()
      {
          _random = new Random(1);
      }
  
      public override int Read(byte[] buffer, int offset, int count)
      {
          _random.NextBytes(buffer);
          Position += count;
          return count;
      }
  
      // other unimportant methods
  }
  </code></pre>
  
  <p>And consistent with examples I've found, a MessageContract for receiving the Streamed upload:</p>
  
  <pre><code>[MessageContract]
  public class ClipUpload : IDisposable
  {
      [MessageHeader(MustUnderstand = true)]
      public long MediaId;
  
      [MessageBodyMember(Order=1)]
      public System.IO.Stream MediaStream;
  
      // disposable implementaton
  }
  </code></pre>
  
  <p>Now, as far as I can tell, my service is properly configured for streamed transfers and the stream can be read on the server-side without issue. Server config of interest:</p>
  
  <pre><code>&lt;basicHttpsBinding&gt;
      &lt;binding name="A" transferMode="Streamed" maxReceivedMessageSize="4294967296" maxBufferSize="65536"&gt;
          &lt;security mode="TransportWithMessageCredential"&gt;
              &lt;message clientCredentialType="UserName" /&gt;
          &lt;/security&gt;
      &lt;/binding&gt;
  &lt;/basicHttpsBinding&gt;
  </code></pre>
  
  <p>Client config of interest:</p>
  
  <pre><code>&lt;basicHttpsBinding&gt;
      &lt;binding name="A" transferMode="Streamed" maxBufferSize="65536"&gt;
          &lt;security mode="TransportWithMessageCredential" /&gt;
      &lt;/binding&gt;
  &lt;/basicHttpsBinding&gt;
  </code></pre>
  
  <p>The server-side operation is simple. In my service interface::</p>
  
  <pre><code>[OperationContract(IsOneWay=true)]
  void UploadClip(ClipUpload upload);
  </code></pre>
  
  <p>Implementation:</p>
  
  <pre><code>public void UploadClip(ClipUpload upload)
  {
      using (var stream = new FileStream(@"C:\Temp\temp.mp4", FileMode.Create, FileAccess.Write))
      {
          upload.MediaStream.CopyTo(stream);
      }
  </code></pre>
  
  <p>The client is also using a simple call:</p>
  
  <pre><code>using(var stream = new InfiniteStream())
  {
      _service.UploadClip(1, stream);
  }
  </code></pre>
  
  <p>Again, the issue is that the client is not sending any of the stream data until the stream is fully read, which of course will never be the case for InfiniteStream. Thus, flow never reaches the server's UploadClip method. For non-"infinite" streams, the process works, but there is a considerable delay while the client is seemingly buffering all of the stream content.</p>