WCF系列:Binding模型 信道工厂(Channel Factory)
来源:互联网 发布:数据库 文件系统 区别 编辑:程序博客网 时间:2024/05/22 13:28
一、信道工厂相关的接口和基类
对于信道监听器,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: }
除了上面的两个接口之外,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: }
模型]之四:信道工厂(Channel Factory)_10966" src="/attachment.aspx?attachmentid=10966">
二、案例演示:如何自定义信道工厂
在上一个案例中,我们创建了一个自定义的信道监听器: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: }
- WCF系列:Binding模型 信道工厂(Channel Factory)
- [WCF的Binding模型]之四:信道工厂(Channel Factory)
- WCF系列:Binding模型 信道监听器(Channel Listener)
- WCF系列:Binding模型 信道与信道栈(Channel and Channel Stack)
- [WCF中的Binding模型]之二: 信道与信道栈(Channel and Channel Stack)
- WCF中的Binding模型之二: 信道与信道栈(Channel and Channel Stack)
- [WCF的Binding模型]之三:信道监听器(Channel Listener)
- WCF系列:Binding模型 绑定元素(Binding Element)
- 信道模型 Channel Modal 小结(2)
- WCF中系列:Binding模型简介
- (二)WCF的Binding模型
- 隐蔽信道(COVERT CHANNEL)
- WCF 绑定(Binding)
- [WCF中的Binding模型]之一: Binding模型简介
- [WCF的Binding模型]之五:绑定元素(Binding Element)
- WCF系列:Binding模型 从绑定元素认识系统预定义绑定
- 无线AP信道(Channel)详解
- FreeSWITCH channel variables(信道变量)
- inotify 中, 在 read 其 struct inotify_event 时, 缓冲区必须大于读取的结构体的大小
- WCF系列:Binding模型 信道监听器(Channel Listener)
- [Ext.Net] 1.2.0_Ext.Net.TreePanel 勾选“纠结”发送给服务器端的方法
- buffer cache —— free buffer waits
- pptp 能连接 但是不能上网
- WCF系列:Binding模型 信道工厂(Channel Factory)
- JAVA设计模式——单例(Singleton)模式 多种实现方法
- Handler线程操作
- configure的交叉编译问题
- ASP脚本限制某IP段对网页的访问
- oracle 查询 两个字段,第一个和第二个字段不为空时,就用第二个字段的值, 否则用第一个字段的
- omGrid,返回页面时间,一长串数字解决办法
- [Ext.Net] 1.2.0/Ext JS_用 Ext JS 增删改 Ext.Net.Store
- HDU2955 Robberies