title: Streaming with WCF and MTOM
body: <p>I am using WCF bindings with streamed transfer mode, for both uploading and downloading binary content to/from a service. I've managed to get it working. I'll include the configuration, contracts, etc for reference.</p>
  
  <p>I've made some tests to benchmark different bindings and encodings. Uploading results seems ok. NetTcp being the fastest, followed by BasicHttp-MTOM and then BasicHttp-Text. What suprises me is that, when downloading large files, MTOM is very slow as opposed to Text encoding with BasicHttp and Binary encoding with NetTcp.</p>
  
  <p>Am I missing something? Why BasicHttp-MTOM works way slower than other bindings when uploading? Beside that I've implemented double buffering for downloads. This also works well with all bindings except BasicHttp with MTOM encoding. Why double buffering doesn't help when using MTOM?</p>
  
  <p>Thanks for reading, your advice and ideas about this.</p>
  
  <p>Test Results:</p>
  
  <p>Uploading 150 MB binary data to service. Client creates a file stream from a 150 MB file and passes to server. Server reads the stream into a memory stream. No double buffer yet. Results seems fast as there is no writing of data to file system. And bindings perform as expected.</p>
  
  <p><img src="https://i.stack.imgur.com/SrphU.png" alt="Upload"></p>
  
  <p>Downloading 100 MB binary data from service. Service creates a memory stream and passes to client. Client writes to file system. Here is the results with both single and double buffer. As you can see 
  MTOM seems extremely slow and doesn't respond to double buffering as well.</p>
  
  <p><img src="https://i.stack.imgur.com/JOyzR.png" alt="Download"></p>
  
  <p>Server configuration (left out some parts for simplicity):</p>
  
  <pre><code>&lt;configuration&gt;
    &lt;system.web&gt;
      &lt;httpRuntime maxRequestLength="2147483647"/&gt;
    &lt;/system.web&gt;
    &lt;system.serviceModel&gt;
      &lt;bindings&gt;
        &lt;netTcpBinding&gt;
          &lt;binding name="StreamedNetTcpBinding"
                   transferMode="Streamed"
                   maxReceivedMessageSize="1099511627776"&gt;
          &lt;/binding&gt;
        &lt;/netTcpBinding&gt;
        &lt;basicHttpBinding&gt;
          &lt;binding name="StreamedBasicHttpBindingWithMtom"
                   messageEncoding="Mtom" transferMode="Streamed"
                   maxReceivedMessageSize="1099511627776"&gt;
          &lt;/binding&gt;
          &lt;binding name="StreamedBasicHttpBinding"
                   transferMode="Streamed"
                   maxReceivedMessageSize="1099511627776"&gt;
          &lt;/binding&gt;
        &lt;/basicHttpBinding&gt;
      &lt;/bindings&gt;
    &lt;/system.serviceModel&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>Client configuration (left out some parts for simplicity):</p>
  
  <pre><code>&lt;configuration&gt;
    &lt;system.serviceModel&gt;
      &lt;bindings&gt;
        &lt;basicHttpBinding&gt;
          &lt;binding name="StreamedBasicHttpBindingWithMtom" 
                   maxReceivedMessageSize="1099511627776"
                   messageEncoding="Mtom" transferMode="Streamed"&gt;
          &lt;/binding&gt;
          &lt;binding name="StreamedBasicHttpBinding"
                   maxReceivedMessageSize="1099511627776"
                   transferMode="Streamed"&gt;
          &lt;/binding&gt;
        &lt;/basicHttpBinding&gt;
        &lt;netTcpBinding&gt;
          &lt;binding name="StreamedNetTcpBinding" transferMode="Streamed"
            maxReceivedMessageSize="1099511627776"&gt;
          &lt;/binding&gt;
        &lt;/netTcpBinding&gt;
      &lt;/bindings&gt;
    &lt;/system.serviceModel&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>Service Contract:</p>
  
  <pre><code>[ServiceContract]
  public interface IFileService
  {
      [OperationContract]
      void UploadFile(DocumentData document);
  
      [OperationContract]
      DocumentData DownloadFile();
  }
  </code></pre>
  
  <p>Message Contract:</p>
  
  <pre><code>[MessageContract]
  public class DocumentData
  {
      [MessageHeader(MustUnderstand = true)]
      public string DocumentName { get; set; }
  
      [MessageHeader(MustUnderstand = true)]
      public int FileLength { get; set; }
  
      [MessageBodyMember(Order = 1)]
      public Stream Data { get; set; }
  }
  </code></pre>
  
  <p><hr>
  Edit: This turned out to be an issue with my development environment setup at work. When I ran the same tests at home, the results was as expected.</p>
  
  <p><img src="https://i.stack.imgur.com/oxAak.png" alt="Download"></p>