WCF系列:Binding模型 信道工廠(Channel Factory)

由於信道管理器在客戶端和服務端所起的不同作用,分爲信道監聽器和信道工廠。和服務端的信道監聽其相比,處於客戶端的信道工廠顯得簡單。從名稱就可以看得出來,信道工廠的作用就是單純的創建用於消息發送的信道。我們先來看看與信道工廠相關的一些接口和基類的定義。

一、信道工廠相關的接口和基類

對於信道監聽器,WCF定義了兩個接口:IChannelListener和IChnnelListener<TChannel>。與之相對地,WCF也爲信道工廠定義了兩個接口:IChannelFactory和IChannelFactory<TChannel>。這兩個接口定義了信道工廠最基本的功能和屬性,下面是這兩個接口的定義:
  1.   1: public interface IChannelFactory : ICommunicationObject
  2.   2: {
  3.   3:    // Methods
  4.   4:    T GetProperty<T>() where T : class;
  5.   5: }
  6.   6: public interface IChannelFactory<TChannel> : IChannelFactory, ICommunicationObject
  7.   7: {
  8.   8:    // Methods
  9.   9:    TChannel CreateChannel(EndpointAddress to);
  10. 10:    TChannel CreateChannel(EndpointAddress to, Uri via);
  11. 11: }
複製代碼
由於信道工廠的目的就是單純的創建信道,所以IChannelFactory和IChannelFactory<TChannel>的定義顯得格外簡潔。兩個重載的CreateChannel方法通過目的終結點的地址(to),以及在手工尋址下不同於目的終結點地址的另一個地址,該地址是消息實際會被髮送的地址(via)。關於To和Via可以參考第二章關於物理地址和邏輯地址的部分。

除了上面的兩個接口之外,WCF還定義分別是實現了它們的兩個抽象基類:ChannelFactoryBase和ChannelFactoryBase<TChannel>。

ChannelFactoryBase繼承自所有信道管理器的基類:CnannelManagerBase,而ChannelManagerBase又繼承自CommunicationObject,實現ICommunicationObject接口定義的基本的狀態屬性和狀態轉換功能。並且實現了接口IChannelFactory和ICommunicationObject。而ChannelFactoryBase<TChannel>繼承自CnannelManagerBase,並且實現了接口:IChannelFactory<TChannel>, IChannelFactory和ICommunicationObject。一般地,範型類型TChannel爲基於相應channel shape下客戶端信道類型,比如IOutputChannel、IRequestChannel和IDuplexChannel。ChannelFactoryBase和ChannelFactoryBase<TChannel>的簡單定義如下:
  1.   1: public abstract class ChannelFactoryBase : ChannelManagerBase, IChannelFactory, ICommunicationObject
  2.   2: {
  3.   3:    ......
  4.   4: }
  5.   5: public abstract class ChannelFactoryBase<TChannel> : ChannelFactoryBase, IChannelFactory<TChannel>, IChannelFactory, ICommunicationObject
  6.   6: {
  7.   7:    ......
  8.   8: }
複製代碼
下面的類圖簡明直觀的表述了WCF中關於信道工廠的體系結構。
模型]之四:信道工廠(Channel Factory)_10966" src="/attachment.aspx?attachmentid=10966">
image_2.png(87.20 K)
12/5/2008 8:46:39 PM



二、案例演示:如何自定義信道工廠

在上一個案例中,我們創建了一個自定義的信道監聽器:SimpleReplyChannelListner。該信道監聽器用於在請求-回覆消息交換模式下進行請求的監聽。在本案例中,我們來創建與之相對的信道工廠:SimpleChannelFactory<TChannel>,用於請求-回覆消息交換模式下進行用於請求發送信道的創建。由於SimpleChannelFactory<TChannel>的實現相對簡單,將所有代碼一併附上。

SimpleChannelFactory<TChannel>直接繼承自抽象基類SimpleChannelFactoryBase<TChannel>。字段成員_innerChannelFactory表示信道工廠棧中後一個信道工廠對象,該成員在構造函數中通過傳入的BindingContext對象的BuildInnerChannelFactory<TChannel>方法創建。OnCreateChannel是核心大方法,實現了真正的信道創建過程,在這裏我們創建了我們自定義的信道:SimpleRequestChannel.。構建SimpleRequestChannel. 的InnerChannel通過  _innerChannelFactory的CreateChannel方法創建。對於其他的方法(OnOpen、OnBeginOpen和OnEndOpen),我們僅僅通過PrintHelper輸出當前的方法名稱,並調用 _innerChannelFactory相應的方法。

1: public class SimpleChannelFactory<TChannel> : ChannelFactoryBase<TChannel>
  2: {
  3:    public IChannelFactory<TChannel> _innerChannelFactory;
  4:
  5:    public SimpleChannelFactory(BindingContext context)
  6:    {
  7:        PrintHelper.Print(this, "SimpleChannelFactory");
  8:        this._innerChannelFactory = context.BuildInnerChannelFactory<TChannel>();
  9:    }
10:
11:    protected override TChannel OnCreateChannel(EndpointAddress address, Uri via)
12:    {
13:        PrintHelper.Print(this, "OnCreateChannel");
14:        IRequestChannel innerChannel = this._innerChannelFactory.CreateChannel(address, via) as IRequestChannel;
15:        SimpleRequestChannel. channel = new SimpleRequestChannel.(this, innerChannel);
16:        return (TChannel)(object)channel;
17:    }
18:
19:    protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
20:    {
21:        PrintHelper.Print(this, "OnBeginOpen");
22:        return this._innerChannelFactory.BeginOpen(timeout, callback, state);
23:    }
24:
25:    protected override void OnEndOpen(IAsyncResult result)
26:    {
27:        PrintHelper.Print(this, "OnEndOpen");
28:        this._innerChannelFactory.EndOpen(result);
29:    }
30:
31:    protected override void OnOpen(TimeSpan timeout)
32:    {
33:        PrintHelper.Print(this, "OnOpen");
34:        this._innerChannelFactory.Open(timeout);
35:    }
36: }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章