WCF信道工廠接口與相關(guān)基類描述
WCF開發(fā)框架中有一種叫做WCF信道工廠的東西。對于剛剛接觸WCF不久的朋友可能對其還不太了解。由于信道管理器在客戶端和服務(wù)端所起的不同作用,分為信道監(jiān)聽器和信道工廠。#t#
和服務(wù)端的信道監(jiān)聽其相比,處于客戶端的信道工廠顯得簡單。從名稱就可以看得出來,WCF信道工廠的作用就是單純的創(chuàng)建用于消息發(fā)送的信道。我們先來看看與信道工廠相關(guān)的一些接口和基類的定義。
一、WCF信道工廠相關(guān)的接口和基類
對于信道監(jiān)聽器,WCF定義了兩個接口:IChannelListener和IChnnelListener< TChannel>。與之相對地,WCF也為信道工廠定義了兩個接口:IChannelFactory和IChannelFactory< TChannel>。這兩個接口定義了信道工廠最基本的功能和屬性,下面是這兩個接口的定義:
- public interface IChannelFactory
: ICommunicationObject - {
- // Methods
- T GetProperty< T>() where T : class;
- }
- public interface IChannelFactory< TChannel>
: IChannelFactory, ICommunicationObject - {
- // Methods
- TChannel CreateChannel(EndpointAddress to);
- TChannel CreateChannel(EndpointAddress
to, Uri via); - }
由于WCF信道工廠的目的就是單純的創(chuàng)建信道,所以IChannelFactory和IChannelFactory< TChannel>的定義顯得格外簡潔。兩個重載的CreateChannel方法通過目的終結(jié)點的地址(to),以及在手工尋址下不同于目的終結(jié)點地址的另一個地址,該地址是消息實際會被發(fā)送的地址(via)。關(guān)于To和Via可以參考第二章關(guān)于物理地址和邏輯地址的部分。
除了上面的兩個接口之外,WCF還定義分別是實現(xiàn)了它們的兩個抽象基類:ChannelFactoryBase和ChannelFactoryBase< TChannel>。
ChannelFactoryBase繼承自所有信道管理器的基類:CnannelManagerBase,而ChannelManagerBase又繼承自CommunicationObject,實現(xiàn)ICommunicationObject接口定義的基本的狀態(tài)屬性和狀態(tài)轉(zhuǎn)換功能。并且實現(xiàn)了接口IChannelFactory和ICommunicationObject。而ChannelFactoryBase< TChannel>繼承自CnannelManagerBase,并且實現(xiàn)了接口:IChannelFactory< TChannel>, IChannelFactory和ICommunicationObject。
一般地,范型類型TChannel為基于相應(yīng)channel shape下客戶端信道類型,比如IOutputChannel、IRequestChannel和IDuplexChannel。ChannelFactoryBase和ChannelFactoryBase< TChannel>的簡單定義如下:
- public abstract class ChannelFactoryBase
: ChannelManagerBase, IChannelFactory,
ICommunicationObject- {
- ......
- }
- public abstract class ChannelFactoryBase
< TChannel> : ChannelFactoryBase, IChannel
Factory< TChannel>, IChannelFactory,
ICommunicationObject- {
- ......
- }
二、案例演示:如何自定義WCF信道工廠
在上一個案例中,我們創(chuàng)建了一個自定義的信道監(jiān)聽器:SimpleReplyChannelListner。該信道監(jiān)聽器用于在請求-回復(fù)消息交換模式下進行請求的監(jiān)聽。在本案例中,我們來創(chuàng)建與之相對的信道工廠:SimpleChannelFactory< TChannel>,用于請求-回復(fù)消息交換模式下進行用于請求發(fā)送信道的創(chuàng)建。由于SimpleChannelFactory< TChannel>的實現(xiàn)相對簡單,將所有代碼一并附上。
SimpleChannelFactory< TChannel>直接繼承自抽象基類SimpleChannelFactoryBase< TChannel>。字段成員_innerChannelFactory表示信道工廠棧中后一個信道工廠對象,該成員在構(gòu)造函數(shù)中通過傳入的BindingContext對象的BuildInnerChannelFactory< TChannel>方法創(chuàng)建。
OnCreateChannel是核心大方法,實現(xiàn)了真正的信道創(chuàng)建過程,在這里我們創(chuàng)建了我們自定義的信道:SimpleRequestChannel.。構(gòu)建SimpleRequestChannel. 的InnerChannel通過_innerChannelFactory的CreateChannel方法創(chuàng)建。對于其他的方法(OnOpen、OnBeginOpen和OnEndOpen),我們僅僅通過PrintHelper輸出當前的方法名稱,并調(diào)用_innerChannelFactory相應(yīng)的方法。
- public class SimpleChannelFactory< TChannel>
: ChannelFactoryBase< TChannel>- {
- public IChannelFactory< TChannel> _innerChannelFactory;
- public SimpleChannelFactory(BindingContext context)
- {
- PrintHelper.Print(this, "SimpleChannelFactory");
- this._innerChannelFactory = context.
BuildInnerChannelFactory< TChannel>();- }
- protected override TChannel OnCreateChannel
(EndpointAddress address, Uri via)- {
- PrintHelper.Print(this, "OnCreateChannel");
- IRequestChannel innerChannel = this._
innerChannelFactory.CreateChannel(address,
via) as IRequestChannel;- SimpleRequestChannel. channel = new
SimpleRequestChannel.(this, innerChannel);- return (TChannel)(object)channel;
- }
- protected override IAsyncResult OnBeginOpen
(TimeSpan timeout, AsyncCallback callback,
object state)- {
- PrintHelper.Print(this, "OnBeginOpen");
- return this._innerChannelFactory.BeginOpen
(timeout, callback, state);- }
- protected override void OnEndOpen(IAsyncResult result)
- {
- PrintHelper.Print(this, "OnEndOpen");
- this._innerChannelFactory.EndOpen(result);
- }
- protected override void OnOpen(TimeSpan timeout)
- {
- PrintHelper.Print(this, "OnOpen");
- this._innerChannelFactory.Open(timeout);
- }
- }