WCF Streaming流處理相關特點以及應用技巧講解
WCF中有很多比較深奧的內容需要我們在不斷的實踐中去深入研究。比如今天為大家介紹的WCF Streaming流處理,就是其中一個比較難以理解的內容。希望本文介紹的內容能夠給大家帶來一些幫助。#t#
Streaming流處理的特點:
顯然對于處理大量的消息數據而言,流處理機制改善了系統的吞吐量和響應效率。
WCF Streaming流處理操作定義:
WCF Streaming流處理機制需要使用.NET FrameWork定義的Stream類(它是FileStream, NetworkStream, MemoryStream 的父類)。流處理適用一下場景:
- [ServiceContract]
- interface IMyContract
- {
- [OperationContract]
- Stream StreamReply1( );
- [OperationContract]
- void StreamReply2(out Stream stream);
- [OperationContract]
- void StreamRequest(Stream stream);
- [OperationContract(IsOneWay = true)]
- void OneWayStream(Stream stream);
- }
它可以做為返回數據、參數、輸出參數的類型。當然也可以作為單調服務的操作參數。這里使用的參數必須是可序列化的,例如MemoryStream。而FileStream不支持序列化因而不能作為參數或者返回數據的類型。
WCF Streaming流處理與綁定協議:
流處理機制在特定的綁定協議中才能使用,目前是BasicHttpBinding, NetTcpBinding, 和NetNamedPipeBinding 支持流處理模型。但是在默認情況下,WCF禁止流處理模式。
流傳輸模式使用使用TransferMode進行配置,TransferMode為枚舉類型,其定義如下:
- public enum TransferMode
- {
- // Summary:
- // The request and response messages are both buffered.
- Buffered = 0,
- //
- // Summary:
- // The request and response messages are both streamed.
- Streamed = 1,
- //
- // Summary:
- // The request message is streamed and the response message is buffered.
- StreamedRequest = 2,
- //
- // Summary:
- // The request message is buffered and the response message is streamed.
- StreamedResponse = 3,
- }
只有Streamed模式支持2.1中列舉的流處理模式場景。除了直接在服務上配置屬性以外,我們還可以再服務的配置文件里定義流傳輸模式。代碼如下:
- < basicHttpBinding>
- < binding name="basicHttpBinding" receiveTimeout="10:10:10"
transferMode="Streamed" maxReceivedMessageSize="200000">- < /binding>
- < /basicHttpBinding>
- < netTcpBinding>
- < binding name="netTcpBinding" receiveTimeout="10:10:10"
transferMode="Streamed" maxReceivedMessageSize="200000">- < /binding>
- < /netTcpBinding>
此為托管宿主的配置文件,特定的綁定協議,可以配置其傳輸模式。
注意:
WCF Streaming流處理在使用http協議時,其默認消息長度是64K,如果希望增加數據長度,需要在配置文件里重新設置。如: maxReceivedMessageSize="200000",具體代碼如下:
- < basicHttpBinding>
- < binding name="basicHttpBinding" receiveTimeout="10:10:10"
transferMode="Streamed" maxReceivedMessageSize="200000">- < /binding>
- < /basicHttpBinding>
以上就是我們對WCF Streaming流處理的相關介紹。