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

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

对于信道监听器,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);
0:     TChannel CreateChannel(EndpointAddress to, Uri via);
1: }
 

由于信道工厂的目的就是单纯的创建信道,所以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中关于信道工厂的体系结构。

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

在上一个案例中,我们创建了一个自定义的信道监听器: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:     }
0: 
1:     protected override TChannel OnCreateChannel(EndpointAddress address, Uri via)
2:     {
3:         PrintHelper.Print(this, "OnCreateChannel");
4:         IRequestChannel innerChannel = this._innerChannelFactory.CreateChannel(address, via) as IRequestChannel;
5:         SimpleRequestChannel. channel = new SimpleRequestChannel.(this, innerChannel);
6:         return (TChannel)(object)channel;
7:     }
8: 
9:     protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
0:     {
1:         PrintHelper.Print(this, "OnBeginOpen");
2:         return this._innerChannelFactory.BeginOpen(timeout, callback, state);
3:     }
4: 
5:     protected override void OnEndOpen(IAsyncResult result)
6:     {
7:         PrintHelper.Print(this, "OnEndOpen");
8:         this._innerChannelFactory.EndOpen(result);
9:     }
0: 
1:     protected override void OnOpen(TimeSpan timeout)
2:     {
3:         PrintHelper.Print(this, "OnOpen");
4:         this._innerChannelFactory.Open(timeout);
5:     }
6: }
 

WCF中的绑定模型:
[WCF中的Binding模型]之一: Binding模型简介
[WCF中的Binding模型]之二: 信道与信道栈(Channel and Channel Stack)
[WCF中的Binding模型]之三:信道监听器(Channel Listener)
[WCF中的Binding模型]之四:信道工厂(Channel Factory)
[WCF中的Binding模型]之五:绑定元素(Binding Element)
[WCF中的Binding模型]之六:从绑定元素认识系统预定义绑定

最新文章

  1. 使用GIT进行源码管理 —— 在VisualStudio中使用GIT
  2. getPx function
  3. PHP的输出缓冲区(转)
  4. Ceph RGW 和 niginx 配置要点
  5. c语言计算矩阵特征值和特征向量-1(幂法)
  6. XidianOJ 1044 炸金花
  7. Picard 法求方程根
  8. asp.net 多个文件同时下载
  9. [转载] vim带你装逼带你飞(一)
  10. &lt;jsp:invoke fragment=&quot;&quot;/&gt;的理解和使用
  11. ajaxfileupload插件,C#返回Json数据报错
  12. R语言︱函数使用技巧(循环、if族/for、switch、repeat、ifelse、stopifnot)
  13. 关于H5的一些杂思细想(一)
  14. springmvc 整合Controller出现实例化两次问题
  15. Scratch 可能能帮你找到学习编程的初心
  16. Git源码安装 Linux指定安装目录
  17. findpanel 的相关代码
  18. Tf中的NCE-loss实现学习【转载】
  19. 【BZOJ1823】【JSOI2010】满汉全席
  20. Exp4 恶意代码分析 20155223

热门文章

  1. PHP(二)常用函数
  2. SQL Case 语句的使用
  3. 遮罩 HUD 指示器 蒙板 弹窗
  4. SQL——视图、事务、锁、存储过程
  5. layui 前端UI框架
  6. zabbix基础安装
  7. 前端入门22-讲讲模块化 包括webstrom建立简单ES6
  8. Html 内联元素、外联元素 和 可变元素
  9. rsync_ssh
  10. 基于短语的统计机器翻(PBMT) 开源工具 :Moses