WCF Streaming流處理相關(guān)特點(diǎn)以及應(yīng)用技巧講解
WCF中有很多比較深?yuàn)W的內(nèi)容需要我們?cè)诓粩嗟膶?shí)踐中去深入研究。比如今天為大家介紹的WCF Streaming流處理,就是其中一個(gè)比較難以理解的內(nèi)容。希望本文介紹的內(nèi)容能夠給大家?guī)硪恍椭?t#
Streaming流處理的特點(diǎn):
顯然對(duì)于處理大量的消息數(shù)據(jù)而言,流處理機(jī)制改善了系統(tǒng)的吞吐量和響應(yīng)效率。
WCF Streaming流處理操作定義:
WCF Streaming流處理機(jī)制需要使用.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);
- }
它可以做為返回?cái)?shù)據(jù)、參數(shù)、輸出參數(shù)的類型。當(dāng)然也可以作為單調(diào)服務(wù)的操作參數(shù)。這里使用的參數(shù)必須是可序列化的,例如MemoryStream。而FileStream不支持序列化因而不能作為參數(shù)或者返回?cái)?shù)據(jù)的類型。
WCF Streaming流處理與綁定協(xié)議:
流處理機(jī)制在特定的綁定協(xié)議中才能使用,目前是BasicHttpBinding, NetTcpBinding, 和NetNamedPipeBinding 支持流處理模型。但是在默認(rèn)情況下,WCF禁止流處理模式。
流傳輸模式使用使用TransferMode進(jìn)行配置,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中列舉的流處理模式場景。除了直接在服務(wù)上配置屬性以外,我們還可以再服務(wù)的配置文件里定義流傳輸模式。代碼如下:
- < 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>
此為托管宿主的配置文件,特定的綁定協(xié)議,可以配置其傳輸模式。
注意:
WCF Streaming流處理在使用http協(xié)議時(shí),其默認(rèn)消息長度是64K,如果希望增加數(shù)據(jù)長度,需要在配置文件里重新設(shè)置。如: maxReceivedMessageSize="200000",具體代碼如下:
- < basicHttpBinding>
- < binding name="basicHttpBinding" receiveTimeout="10:10:10"
transferMode="Streamed" maxReceivedMessageSize="200000">- < /binding>
- < /basicHttpBinding>
以上就是我們對(duì)WCF Streaming流處理的相關(guān)介紹。


2009-12-22 19:14:36




