This is a mirror of official site: http://jasper-net.blogspot.com/

WCF: Streaming Large Data Files

| Monday, January 24, 2011
WCF can be used to transfer image files, .pdf files or other large documents.  The most common way to do this is to use streaming.

Normally WCF processes messages in buffered mode.  This means incoming messages are not processed until the entire message has been received and buffered in memory.  Likewise, outgoing messages are not sent until the entire message has been buffered in memory.  

Buffered transfer mode is also blocking.  This means the client is blocked until the entire message has been received and processed and the client receives a response.

Normally buffered transfer is fine.  When the messages contain large files, however, buffering creates serious performance problems.  The solution is to stream large messages instead of buffering.  Streaming allows the message recipient (which could be client or the service) to start processing the message before the entire message has been received.  

To use streaming transfer, you need to configure the binding:

   <system.serviceModel>
       <services>
           <service name="MyDocumentService">
               <endpoint
                    binding="basicHttpBinding"
                    bindingConfiguration="DocumentServiceBinding"
                   ...  
               </endpoint>
           </service>
       </services>

       <bindings>
           <basicHttpBinding>
                <binding
                   name="DocumentServiceBinding"
                   messageEncoding="Mtom"
                   transferMode="Streamed"
                   maxBufferSize="65536"
                   maxReceivedMessageSize="5242880"
                   ...  
                   <binding/>
           </basicHttpBinding>
       </bindings>

   </system.serviceModel>

By default transferMode is Buffered.  As shown above, transferMode is Streamed, which means all requests and responses are streamed.  Other options include StreamedRequest (= streamed requests with buffered responses) and StreamedResponse (= buffered requests and streamed responses).

Restrictions

There are a number of important restrictions when you use streaming.

The message body cannot have a digital signature.  A digital signature cannot be checked until the entire message has been received.  That would require buffered transfer.  Likewise, the message cannot be encrypted.  You can still use transport level security such as SSL, but the message itself cannot be encrypted.

WCF reliable messaging requires buffering in order to guarantee that all messages are processed in the correct order.

Read more: C# Corner

Posted via email from Jasper-net

0 comments: