Streaming with WCF and MTOM
Asked Answered
S

1

6

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.

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.

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?

Thanks for reading, your advice and ideas about this.

Test Results:

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.

Upload

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.

Download

Server configuration (left out some parts for simplicity):

<configuration>
  <system.web>
    <httpRuntime maxRequestLength="2147483647"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="StreamedNetTcpBinding"
                 transferMode="Streamed"
                 maxReceivedMessageSize="1099511627776">
        </binding>
      </netTcpBinding>
      <basicHttpBinding>
        <binding name="StreamedBasicHttpBindingWithMtom"
                 messageEncoding="Mtom" transferMode="Streamed"
                 maxReceivedMessageSize="1099511627776">
        </binding>
        <binding name="StreamedBasicHttpBinding"
                 transferMode="Streamed"
                 maxReceivedMessageSize="1099511627776">
        </binding>
      </basicHttpBinding>
    </bindings>
  </system.serviceModel>
</configuration>

Client configuration (left out some parts for simplicity):

<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="StreamedBasicHttpBindingWithMtom" 
                 maxReceivedMessageSize="1099511627776"
                 messageEncoding="Mtom" transferMode="Streamed">
        </binding>
        <binding name="StreamedBasicHttpBinding"
                 maxReceivedMessageSize="1099511627776"
                 transferMode="Streamed">
        </binding>
      </basicHttpBinding>
      <netTcpBinding>
        <binding name="StreamedNetTcpBinding" transferMode="Streamed"
          maxReceivedMessageSize="1099511627776">
        </binding>
      </netTcpBinding>
    </bindings>
  </system.serviceModel>
</configuration>

Service Contract:

[ServiceContract]
public interface IFileService
{
    [OperationContract]
    void UploadFile(DocumentData document);

    [OperationContract]
    DocumentData DownloadFile();
}

Message Contract:

[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; }
}


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.

Download

Sudarium answered 23/11, 2012 at 8:32 Comment(1)
Nice catch with that double buffer although in the link you shared(the one with Thomas Levesque) it is stated that there is no performance difference by using it and even 10% slower in one of the tests (the usage is different but still.. too much performance difference between single/double).Jelle

© 2022 - 2024 — McMap. All rights reserved.