如果您对.NET远程处理或者是从事过有关Socket相关的编程,那么您可能会对以上两个概念不会很陌生.其实信道工厂和信道监听器的概念在.NET远程处理和Socket中就提到过,只是在WCF中的这两个概念更加灵活.那么究竟他们主要用来做什么了? 我可以给您打一个比方,当需要建立通信的时候,首先的必备条件就是需要一个请求发送端,以及一个请求接受端,这就是在分布式编程中的客户端和服务器端,服务器端为客户端的请求提供服务,如果您对通信协议了解的话,可能会知道通信方式一般有三种,单工通信,双工通信和半双工通信.同时在WCF中也涉及到这些概念,那么这些概念具体体现在哪里了? 就是信道工厂和信道监听器.信道工厂和信道监听器只是客户端和服务器端建立通信的容器,它里面包含了信道的形状,以及中介点,承载服务的资源描述以及绑定,虽然微软在发布WCF时说给了开发人员足够的灵活性,实事上是要大打折扣的.当在创建自定义的信道工厂和信道监听器时,只是重载了一些方法.我觉得这对于要扩展使用WCF这已经足够了.同时我们还需要了解信道工厂的这种机制,当上层服务需要使用新到工厂发送请求时,信道工厂会创建一个内部的信道工厂,而自己继续等待并为上层服务.这种机制有些像IIS中的应用程序池.说道这就可以看具体实现的代码了:


2 /// 继承自ChannelFactory实现自主创建信道工厂,在绑定元素中的创建信道工厂使用
3 /// </summary>
4 /// <typeparam name="TChannel"></typeparam>
5 class MyChannelFactory<TChannel>: ChannelFactoryBase<IRequestChannel>
6 {
7 IChannelFactory<TChannel> innerChannelFactory = null;
8
9 /// <summary>
10 /// 为内部创建的变量信道工厂初始化,使用绑定元素
11 /// </summary>
12 /// <param name="bindingElement"></param>
13 /// <param name="context"></param>
14 public MyChannelFactory(MyBindingElement bindingElement, BindingContext context) : base(context.Binding)
15 {
17 this.innerChannelFactory = context.BuildInnerChannelFactory<TChannel>();
18 if (this.innerChannelFactory == null)
19 {
20 throw new InvalidOperationException("MyCustomChannelFactory requires an inner IChannelFactory.");
21 }
22 }
23
24 //创建请求信道,请求信道基于创建的内部信道工厂
25 protected override IRequestChannel OnCreateChannel(System.ServiceModel.EndpointAddress address, Uri via)
26 {
28 IRequestChannel innerChannel = (IRequestChannel)this.innerChannelFactory.CreateChannel(address, via);
29 return new MyCustomRequestChannel(innerChannel);
30
31 }
32
33 protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
34 {
35 throw new Exception("The method or operation is not implemented.");
36 }
37
38 protected override void OnEndOpen(IAsyncResult result)
39 {
40 throw new Exception("The method or operation is not implemented.");
41 }
42
43 protected override void OnOpen(TimeSpan timeout)
44 {
46 this.innerChannelFactory.Open(timeout);
47 }
48
您可能也发现了,在整个创建信道工厂的时候,主要只是涉及到了几个函数,同时通过这,也可以间接了解到WCF的整个结构.最重要的就是初始化函数和创建信道函数,其他均是为了实现异步操作的扩展,同时在这里也发现了当调用开启信道工厂的时候,并不是开启他本身,而是开启内部创建的信道工厂,此时我觉得如果对信道工厂还有什么疑问的话,就该就是内部如何实现多线程的.这个问题以后有时间再探讨.在构造函数里涉及到了绑定,其实绑定在此处的作用很单一,确定传输协议.那么接着您就又会问绑定上下文,其实微软几乎在他所有的架构技术中会都涉及到上下文,上下文只是记录当先环境中与当前概念相关的信息.要理解上下文必须从整个体系了解.在这个里面信道形状通过范型来实现.接下来介绍关于新到监听器,信道监听器可以间接理解为系统中开启的服务,其实它就是开启的后台服务,他一直在等待客户端的调用.它里面的函数要比信道工厂多用一些,下面是创建自定义信道监听器的代码:


2 {
3 class MyChannelListener<TChannel>: ChannelListenerBase<IReplyChannel> where TChannel: class, IChannel
4 {
5 IChannelListener<TChannel> innerChannelListener = null;
6
7 //创建内部监听信道,又传递过来的绑定上下文创建
8 public MyChannelListener(MyCustomBindingElement bindingElement, BindingContext context)
9 : base(context.Binding)
10 {
12 this.innerChannelListener = context.BuildInnerChannelListener<TChannel>();
13 if (this.innerChannelListener == null)
14 {
15 throw new InvalidOperationException("MyCustomChannelListener requires an inner IChannelFactory.");
16 }
17
18 }
19
20
21
22
23 protected override IReplyChannel OnAcceptChannel(TimeSpan timeout)
24 {
25 throw new Exception("The method or operation is not implemented.");
26 }
27
28 protected override IAsyncResult OnBeginAcceptChannel(TimeSpan timeout, AsyncCallback callback, object state)
29 {
31 return this.innerChannelListener.BeginAcceptChannel(timeout, callback, state);
32 }
33
34 //接受信道
35 protected override IReplyChannel OnEndAcceptChannel(IAsyncResult result)
36 {
38 IReplyChannel replyChannel = ((IReplyChannel)this.innerChannelListener.EndAcceptChannel(result));
39 if (replyChannel == null)
40 {
41 return null;
42 }
43 return new MyCustomReplyChannel(replyChannel);
44 }
45
46 protected override IAsyncResult OnBeginWaitForChannel(TimeSpan timeout, AsyncCallback callback, object state)
47 {
48 throw new Exception("The method or operation is not implemented.");
49 }
50
51 protected override bool OnEndWaitForChannel(IAsyncResult result)
52 {
53 throw new Exception("The method or operation is not implemented.");
54 }
55
56 protected override bool OnWaitForChannel(TimeSpan timeout)
57 {
58 throw new Exception("The method or operation is not implemented.");
59 }
60
61 public override Uri Uri
62 {
63 get
64 {
66 return this.innerChannelListener.Uri;
67 }
68 }
69
70 protected override void OnAbort()
71 {
72 throw new Exception("The method or operation is not implemented.");
73 }
74
75 protected override IAsyncResult OnBeginClose(TimeSpan timeout, AsyncCallback callback, object state)
76 {
77 throw new Exception("The method or operation is not implemented.");
78 }
79
80 protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
81 {
82 throw new Exception("The method or operation is not implemented.");
83 }
84
85 protected override void OnClose(TimeSpan timeout)
86 {
88 this.innerChannelListener.Close(timeout);
89 }
90
91 protected override void OnEndClose(IAsyncResult result)
92 {
93 throw new Exception("The method or operation is not implemented.");
94 }
95
96 protected override void OnEndOpen(IAsyncResult result)
97 {
98 throw new Exception("The method or operation is not implemented.");
99 }
100
101 protected override void OnOpen(TimeSpan timeout)
102 {
104 this.innerChannelListener.Open(timeout);
105
106 }
107 }
108 }
109
在信道工厂中存在创建信道,在这就存在接受信道,他所涉及的其他概念也与信道工厂类似,它与信道工厂唯一的区别在于关闭信道的机制上,对于信道工厂必须等到内部信道关闭后,才能关闭上层的信道工厂.如果您存在一些关于WCF开发和使用的问题,很乐意帮您解决.