title: Streaming data from REST WCF service to an external web service
body: <p>I'm trying to write a WCF web service that essentially boils down to a proxy for an external web service which is not in my control (specifically, a major video sharing site).  I need to be able to send ~10-100 MB of data from a client to my web service, and then send that data along to the external service.</p>
  
  <p>I can do that, but to be able to scale, I'd like it to be streamed as much as possible, instead of reading the entire request body into memory and then POSTing it all in one chunk to the external service.</p>
  
  <p>I have the <code>bindingConfiguration</code> set to <code>transferMode="Streamed"</code> in my config:</p>
  
  <pre><code>&lt;bindings&gt;
    &lt;webHttpBinding&gt;
      &lt;binding name="WebHttpBindingConfig" maxReceivedMessageSize="104857600"
               transferMode="Streamed"
               maxBufferSize="104857600"
               maxBufferPoolSize="104857600"&gt;
      &lt;/binding&gt;
    &lt;/webHttpBinding&gt;
  &lt;/bindings&gt;
  </code></pre>
  
  <p>The service takes a <code>Stream</code> as its only argument:</p>
  
  <pre><code>public abstract class Upload&lt;NetworkID&gt; : IUpload
  {
      protected abstract string DatabaseConnection { get; }
  
      public Stream UploadVideo(Stream stream)
      {
          LogManager.GetLogger("Logger").Trace("Upload starting:");
          try
          {
              HttpWebRequest request;
              request = (HttpWebRequest)WebRequest.Create("http://faketestservice:8800/upload");
              request.Method = "POST";
              request.ContentType = "application/octet-stream";
  
              request.ContentLength = WebOperationContext.Current.IncomingRequest.ContentLength;
              stream.CopyTo(request.GetRequestStream(), 64 * 1024 * 1024);
          }
          catch (Exception ex)
          {
              LogManager.GetLogger("Logger").Trace("caught: " + ex.ToString());
          }
  
          return new MemoryStream(Encoding.UTF8.GetBytes("hello upload"));
      }
  }
  </code></pre>
  
  <p>The service simply takes the incoming request stream and copies it to the outgoing request stream, 64K at a time.</p>
  
  <p>But when I try to send some data (~16MB in my example) to the service through Fiddler, monitoring what gets sent to the external service (in this example, a fake listener run on my home computer), none of the data is sent until all the data is received.</p>
  
  <p>I've tried setting the <code>SendChunked</code> property on the outgoing request to true, but that doesn't change anything.</p>