一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

[WCF的Binding模型]之三:信道監(jiān)聽器(Channel Listener)

信道管理器是信道的創(chuàng)建者,一般來說信道棧的中每個信道對應(yīng)著一個信道管理器?;诓煌南⑻幚淼墓δ?,將我們需要將相應(yīng)的信道按照一定的順序能組織起來構(gòu)成一個信道棧,由于信道本身是由信道管理器創(chuàng)建的,所以信道對應(yīng)的信道管理器也構(gòu)成一個信道管理器棧,棧中信道管理器的順序決定由它所創(chuàng)建信道的順序。

對于WCF的信道層來說,信道管理器在服務(wù)端和客戶端扮演著不同的角色,服務(wù)端的信道管理器在于監(jiān)聽來自客戶端的請求,而客戶端的信道僅僅是單純的創(chuàng)建用于消息發(fā)送的信道。因此,客戶端的消息管理器又稱為信道監(jiān)聽器(Channel Listener),客戶端的信道管理器則成為信道工廠(channel factory)。

在WCF中,所有的信道管理器,不管是位于服務(wù)端的信道監(jiān)聽器還是客戶端的信道工廠,都繼承自一個基類:System.ServiceModel.Channels.ChannelManagerBase。ChannelManagerBase直接繼承自CommunicationObject,并實(shí)現(xiàn)了接口IDefaultCommunicationTimeouts。

public abstract class ChannelManagerBase : CommunicationObject, IDefaultCommunicationTimeouts {     ...... } 

1. 信道監(jiān)聽器(Channel Listener)

其實(shí)我們完全可以把一個WCF應(yīng)用開成是一個普通的基于監(jiān)聽-請求模式的網(wǎng)絡(luò)應(yīng)用,服務(wù)端將監(jiān)聽器綁定到一個或一組URI上進(jìn)行網(wǎng)絡(luò)監(jiān)聽,一旦成功監(jiān)聽到來自客戶端的請求,則接收、處理該請求,如需回復(fù)則發(fā)送回復(fù)回客戶端。在整個過程中,監(jiān)聽器處于核心的地位,而WCF中的信道監(jiān)聽器就起著這樣的作用。

1.1. 關(guān)于信道監(jiān)聽器的監(jiān)聽過程

熟悉網(wǎng)絡(luò)編程的朋友一定會對套節(jié)字應(yīng)用編程接口(Berkeley Sockets API)不會陌生,通過Socket API,我們很容易的創(chuàng)建基于網(wǎng)絡(luò)監(jiān)聽-請求的應(yīng)用程序。在.NET編程環(huán)境下,我們將System.NET.Sockets.TcpListener 或者System.NET.Sockets.Socket 對象綁定到一個URI上,讓他們監(jiān)聽來自客戶端的連接。當(dāng)連接請求被成功監(jiān)測到,調(diào)用Accept相關(guān)方法或者方法創(chuàng)建一Socket或者TcpClient對象,并通過這些對象獲得請求消息。

WCF中的信道監(jiān)聽器與之相似。當(dāng)我們對一個服務(wù)進(jìn)行寄宿的時候,會為之添加一個或者多個終結(jié)點(diǎn)。對于一個終結(jié)點(diǎn)來說,它具有一個代表邏輯地址的終結(jié)點(diǎn)地址,還有一個代表物理地址的監(jiān)聽地址(關(guān)于邏輯地址和物理地址,請參閱第二章),如果監(jiān)聽地址(ListenUri)沒有顯式地指定,則監(jiān)聽地址和邏輯地址共享相同的URI。對于每一個不同監(jiān)聽地址,WCF會通過具體的綁定對象創(chuàng)建一個信道監(jiān)聽器。信道監(jiān)聽器通過調(diào)用AcceptChannel創(chuàng)建監(jiān)聽信道棧,位于信道棧的第一個信道被成功返回。

一旦消息請求被成功監(jiān)聽,如果該信道是InputChannel(數(shù)據(jù)報MEP) 或者DuplexChannel(雙工MEP),則調(diào)用Receive或者BeginReceive方法接收消息,如果需要向?qū)ο蟀l(fā)送消息,則通過Send或者BeginSend將消息發(fā)給請求者;如果信道是ReplyChannel(請求/回復(fù)MEP)則調(diào)用ReceiveRequest方法獲得一個RequestContext對象,通過該對象獲取請求消息并發(fā)送回復(fù)消息。

1.2. 信道監(jiān)聽器相關(guān)的接口和基類

由于信道監(jiān)聽器是位于服務(wù)端的信道管理器,所以所有的信道監(jiān)聽器均繼承自基類:ChannelManagerBase。同時由于信道監(jiān)聽器具有其特殊的請求監(jiān)聽的功能,所以WCF還定義一些相關(guān)的接口,比如System.ServiceModel.Channels.IChannelListener和System.ServiceModel.Channels.IChannelListener。

IChannelListener繼承自ICommnucationObject接口。定義了一組WaitForChannel和BeginWaitForChannel/EndWaitForChannel以同步和異步的方式判斷是否具有一個可用的信道;GetProperty和IChannel的GetProperty相對;Uri屬性返回真正的監(jiān)聽地址。

public interface IChannelListener : ICommunicationObject{    IAsyncResult BeginWaitForChannel(TimeSpan timeout, AsyncCallback callback, object state);    bool EndWaitForChannel(IAsyncResult result);    T GetProperty() where T : class;    bool WaitForChannel(TimeSpan timeout);    Uri Uri { get; }}

范型類型的IChannelListener繼承自IChannelListener,范型類型TChannel是一個實(shí)現(xiàn)了IChannel的類,一般來說,TChannel代表基于某種channel shape的Channel, 比如實(shí)現(xiàn)了IOutputChannel、IInputChannel、IRequestChanne、IReplyChannel、IDuplexChannel的IChannel類型。定義在IChannelListener的AcceptChannel和BeginAcceptChannel/EndAcceptChannel在連接請求被監(jiān)聽到時,以同步或者異步的方式創(chuàng)建信道棧用于消息的接收。

public interface IChannelListener : IChannelListener, ICommunicationObject where TChannel : class, IChannel{    // Methods    TChannel AcceptChannel();    TChannel AcceptChannel(TimeSpan timeout);    IAsyncResult BeginAcceptChannel(AsyncCallback callback, object state);    IAsyncResult BeginAcceptChannel(TimeSpan timeout, AsyncCallback callback, object state);    TChannel EndAcceptChannel(IAsyncResult result);}  

除了定義兩個接口外,WCF中還定義了與這兩個接口向?qū)?yīng)的抽象基類:System.ServiceModel.Channels.ChannelListenerBase和System.ServiceModel.Channels.ChannelListenerBase。ChannelListenerBase實(shí)現(xiàn)了接口IChannelListener,而ChannelListenerBase實(shí)現(xiàn)了接口IChannelListener。

public abstract class ChannelListenerBase : ChannelManagerBase, IChannelListener, ICommunicationObject{     ... ...}public abstract class ChannelListenerBase : ChannelListenerBase, IChannelListener, IChannelListener, ICommunicationObject where TChannel : class, IChannel{    ... ...} 

圖3-13所示的類圖大體上表示了上述的這些基類和接口之間的關(guān)系:

image

圖3-13 信道監(jiān)聽器接口與基類

1.3. 案例演示3-3:如何自定義信道監(jiān)聽器

在上面一節(jié)的案例演示中,我們創(chuàng)建了兩個用于請求-回復(fù)消息交換模式下的自定義信道,一個是實(shí)現(xiàn)了IRequestChannel的SimpleRequestChannel.,另一個是實(shí)現(xiàn)了IReplyChannel的SimpleReplyChannel。在本案例以及接下來的案例演示中,我們將為這兩個自定義創(chuàng)建兩個相應(yīng)的信道管理器,其實(shí)一個是用于創(chuàng)建SimpleRequestChannel的自定義信道工廠,另一個則是創(chuàng)建SimpleReplyChannel的自定義信道監(jiān)聽器。先來看看我們自定義的信道監(jiān)聽器SimpleChannelListener。該類繼承自范型的ChannelListenerBase:

public class SimpleChannelListener : ChannelListenerBase where TChannel : class, IChannel{    ... ...} 

我們說過信道一般不會孤立地存在,而是存在于一個由多個信道按照一定順序構(gòu)成的信道棧中。由于信道管理器是信道的締造者,要創(chuàng)建整個信道棧,同樣需要這些信道對應(yīng)的信道管理器按照相應(yīng)的順序組成一個信道管理器棧。反映在具體實(shí)現(xiàn)上,當(dāng)執(zhí)行了某個方法之后,需要調(diào)用棧中后一個信道監(jiān)聽器相應(yīng)的方法,所以在SimpleChannelListener中,定義一個字段_innerChanneListener,代表?xiàng)V信c之相鄰的信道監(jiān)聽器。_innerChanneListener通過在構(gòu)造函數(shù)中指定的BindingContext對象創(chuàng)建。關(guān)于BindingContext,我將在后面的一節(jié)中左詳細(xì)的介紹。

public class SimpleChannelListener : ChannelListenerBase where TChannel : class, IChannel{    ... ...    private IChannelListener _innerChanneListener;     public SimpleChannelListener(BindingContext context)    {        PrintHelper.Print(this, "SimpleChannelListener");        this._innerChanneListener = context.BuildInnerChannelListener();    }} 

對于SimpleChannelListener來說,它的最重要的功能就是創(chuàng)建我們自定義的ReplyChannel:SimpleReplyChannel。SimpleReplyChannel的創(chuàng)建實(shí)現(xiàn)在OnAcceptChannel和OnEndAcceptChannel方法中。在構(gòu)造SimpleReplyChannel的innerChannel通過_innerChanneListener的AcceptChannel方法創(chuàng)建。

public class SimpleChannelListener : ChannelListenerBase where TChannel : class, IChannel{    ... ...     protected override TChannel OnAcceptChannel(TimeSpan timeout)    {        PrintHelper.Print(this, "OnAcceptChannel");        IReplyChannel innerChannel = this._innerChanneListener.AcceptChannel(timeout) as IReplyChannel;        return new SimpleReplyChannel(this, innerChannel) as TChannel;    }    protected override IAsyncResult OnBeginAcceptChannel(TimeSpan timeout, AsyncCallback callback, object state)    {        PrintHelper.Print(this, "OnBeginAcceptChannel");        return this._innerChanneListener.BeginAcceptChannel(timeout, callback, state);     }     protected override TChannel OnEndAcceptChannel(IAsyncResult result)    {        PrintHelper.Print(this, "OnEndAcceptChannel");        return new  SimpleReplyChannel(this,this._innerChanneListener.EndAcceptChannel(result) as IReplyChannel) as TChannel;    }} 

對于定義在基類必須實(shí)現(xiàn)的抽象方法來說,為了簡單起見,我們僅僅是通過PrintHelper輸出當(dāng)前執(zhí)行的方法名稱,然后調(diào)用_innerChanneListener的相應(yīng)的方法就可以了:

public class SimpleChannelListener : ChannelListenerBase where TChannel : class, IChannel{    ... ...         protected override IAsyncResult OnBeginWaitForChannel(TimeSpan timeout, AsyncCallback callback, object state)        {            PrintHelper.Print(this, "OnBeginWaitForChannel");            return this._innerChanneListener.BeginWaitForChannel(timeout, callback, state);         }         protected override bool OnEndWaitForChannel(IAsyncResult result)        {            PrintHelper.Print(this, "OnEndWaitForChannel");            return this._innerChanneListener.EndWaitForChannel(result);        }         protected override bool OnWaitForChannel(TimeSpan timeout)        {            PrintHelper.Print(this, "OnWaitForChannel");            return this._innerChanneListener.WaitForChannel(timeout);        }    ... ...}

WCF中的綁定模型:
[WCF中的Binding模型]之一: Binding模型簡介
[WCF中的Binding模型]之二: 信道與信道棧(Channel and Channel Stack)
[WCF中的Binding模型]之三:信道監(jiān)聽器(Channel Listener)
[WCF中的Binding模型]之四:信道工廠(Channel Factory)
[WCF中的Binding模型]之五:綁定元素(Binding Element)
[WCF中的Binding模型]之六:從綁定元素認(rèn)識系統(tǒng)預(yù)定義綁定

NET技術(shù)[WCF的Binding模型]之三:信道監(jiān)聽器(Channel Listener),轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 夜色资源站www国产在线观看 | 5g影院天天5g爽天天看 | 深夜福利一区 | 国产精品美女免费视频观看 | 91国高清视频 | 亚洲欧洲在线观看 | 亚洲美日韩 | 91最新免费观看在线 | 精品久久久久久久一区二区手机版 | 亚洲 图片 小说 欧美 另类 | 综合伊人久久在一二三区 | 亚洲看黄 | 国产男女免费完整视频 | 日本高清一区二区三区水蜜桃 | 91精品综合 | 91视频社区 | 91大西瓜国产线观看免费 | 亚洲天堂久久精品 | 丝袜亚洲综合 | 国产精品第页 | 韩日成人| 日本vs欧美一区二区三区 | 人人干在线观看 | 国产zzzwww在线观看 | 天堂中文在线资源 | 大臿蕉香蕉大视频成人 | 亚洲一区中文 | 好吊妞视频998www | 国产精品日本不卡一区二区 | 日本综合欧美一区二区三区 | 四虎4hu永久免费 | 69免费在线视频 | 在线视频精品一区 | 一区二区视频在线免费观看 | 久久精品国产91久久麻豆自制 | 亚州色吧| 欧美激情五月 | 国产极品久久 | 看全色黄大色大片免费久久久 | 日韩欧美一区二区三区中文精品 | 操亚洲 |