技术开发 频道

建立可绑定的WCF通道



六、将通道绑定在一起


    Binding
由很多BindingElements组成。前面提到过,BindingElements建立ChannelListeners。在这个例子中,每一个BindingElement负责一个特殊的ChannelListener。因此,在BindingElement中大多数的方法是BindChannelListener

public override IChannelListener<TChannel> BuildChannelListener<TChannel>(BindingContext context) { if (context == null) { throw new ArgumentNullException("context"); } if (!CanBuildChannelListener<TChannel>(context)) { throw new ArgumentException(String.Format("Unsupported channel type: {0}.", typeof(TChannel).Name)); } BigHelper.DisplayMessage("Build Listener " + this.ToString() + " of type " + typeof(TChannel).ToString() ); return (IChannelListener<TChannel>)(object)new TestTransportChannelListener(this, context); }

当我们看下一部分时,会发现WCF开始读取Binding,并从BindingElement集合中建立通道栈。

七、调用通道栈
    本例子中的代码将执行以下动作:
1. 建立一个新的Binding。
2. 从通道栈的栈顶获得ChannelListener。
3. 打开最顶端的ChannelListener。
4. 调用AcceptChannel,并依次调用最顶端的ChannelListener的OnAcceptChannel和返回最顶端的通道。
5. 打开最顶端的通道。
6. 调用最顶端通道的Receive方法,并打印返回的消息。

实现代码如下:

static void Main(string[] args) { TestBinding bind = new TestBinding(); IChannelListener<IInputChannel> listener; IInputChannel channel; Message msg; BindingParameterCollection parms = new BindingParameterCollection(); BigHelper.DisplayComment("Tracing..."); BigHelper.DisplayComment(""); listener = bind.BuildChannelListener<IInputChannel>(parms); listener.Open(); channel = listener.AcceptChannel(); channel.Open(); channel.WaitForMessage(TimeSpan.FromSeconds(3.0)); msg = channel.Receive();

 

 

 



从上面代码可以看出, 我们调用了最顶端的监听器和通道的AcceptChannel,并贯穿于整个通道层,直到传输层通道。

八、总结

    本文的主要目的解释了一些和WCF 通道以及绑定相关的问题。WCF是微软制造的一个非常巨大的企业级系统,主要目的是巩固.NET Framework在通讯领域的地位。很多未来的产品将被建立在它之上。因此,学习WCF对于想提前掌握未来技术的开发人员是非常重要的。
0
相关文章