[WCF的Binding模型]之四:信道工厂(Channel Factory)

由于信道管理器在客户端和服务端所起的不同作用,分为信道监听器和信道工厂。和服务端的信道监听其相比,处于客户端的信道工厂显得简单。从名称就可以看得出来,信道工厂的作用就是单纯的创建用于消息发送的信道。我们先来看看与信道工厂相关的一些接口和基类的定义。

一、信道工厂相关的接口和基类

对 于信道监听器,WCF定义了两个接口:IChannelListener和IChnnelListener<TChannel>。与之相对 地,WCF也为信道工厂定义了两个接口:IChannelFactory和IChannelFactory<TChannel>。这两个接口 定义了信道工厂最基本的功能和属性,下面是这两个接口的定义:

  1: public interface IChannelFactory : ICommunicationObject
  2: {
  3:     // Methods
  4:     T GetProperty<T>() where T : class;
  5: }
  6: public interface IChannelFactory<TChannel> : IChannelFactory, ICommunicationObject
  7: {
  8:     // Methods
  9:     TChannel CreateChannel(EndpointAddress to);
 10:     TChannel CreateChannel(EndpointAddress to, Uri via);
 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: public abstract class ChannelFactoryBase : ChannelManagerBase, IChannelFactory, ICommunicationObject
  2: {
  3:     ......
  4: }
  5: public abstract class ChannelFactoryBase<TChannel> : ChannelFactoryBase, IChannelFactory<TChannel>, IChannelFactory, ICommunicationObject
  6: {
  7:     ......
  8: } 

下面的类图简明直观的表述了WCF中关于信道工厂的体系结构。

image

二、案例演示:如何自定义信道工厂

在上一个案例中,我们创建了一个自定义的信道监听器: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: }

 

skyworld -
共有0个回答