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

来源:互联网 发布:数据库 文件系统 区别 编辑:程序博客网 时间:2024/05/22 13:28
由于信道管理器在客户端和服务端所起的不同作用,分为信道监听器和信道工厂。和服务端的信道监听其相比,处于客户端的信道工厂显得简单。从名称就可以看得出来,信道工厂的作用就是单纯的创建用于消息发送的信道。我们先来看看与信道工厂相关的一些接口和基类的定义。

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

对于信道监听器,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中关于信道工厂的体系结构。
[WCF的Binding<span href=模型]之四:信道工厂(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: }

原创粉丝点击