(二)、WCF通道模型

通道模型概述

通道堆栈是具有一个或多个消息处理通道的分层的通信堆栈。堆栈底部是传输通道,它负责使通道堆栈适应基础传输(例如,TCPHTTPSMTP 和其他类型的传输)。通道为消息的发送和接收提供了一个低级编程模型。此编程模型依赖于多个接口和其他类型模型(统称为 WCF 通道模型)。

源文档 <http://msdn.microsoft.com/zh-cn/library/ms729840(v=VS.100).aspx>

通道堆栈

(二)、WCF通道模型

底部通道称为传输通道。它是负责与其他方之间发送和接收消息的通道。这包括负责在与用于和其他方通信的格式之间转换 Message 对象。传输通道上面可以有任意个协议通道,每个协议通道负责提供一种通信功能(如可靠的传递保证)。协议通道对以 Message 对象的形式流过其中的消息执行操作。协议通道通常会转换消息(例如,通过添加标头或加密正文),或者发送和接收协议通道自己的控制消息(例如回执确认)。

通道形状

每个通道均实现一个或多个接口,称为通道形状接口或通道形状

。通道形状的最底部是 IChannel 接口,该接口提供一个 GetProperty<T> 方法,用作访问由堆栈中的通道公开的任意功能的分层机制。扩展 IChannel 的五种通道形状。

另外,这些形状中的每个形状均有一个扩展 System.ServiceModel.Channels.ISessionChannel 以支持会话的等效项。这些等效项是:

通道堆栈的编程

通道堆栈通常是使用工厂模式创建的,在这种模式中,绑定创建通道堆栈。在发送端,使用绑定生成 ChannelFactory,而后者生成通道堆栈并返回对堆栈中顶部通道的引用。之后,应用程序可以使用此通道发送消息

在接收端,使用绑定生成 IChannelListener,用于侦听传入消息。IChannelListener 通过创建通道堆栈并将应用程序引用传递给顶部通道,将消息提供给侦听应用程序。之后,应用程序使用此通道接收传入消息。

通道对象模型

通道对象模型是实现通道、通道侦听器和通道工厂所必需的一组核心接口。还提供一些基类以辅助自定义实现。

ICommunicationObject 是定义所有通信对象实现的基本状态机的核心接口。CommunicationObject 提供了此核心接口的实现,可以从此实现派生其他通道类而不必重新实现接口。但这并不是必需的:自定义通道可以直接实现 ICommunicationObject 而不继承自 CommunicationObject

一种由六种可用状态,分别为OpeningCreatedOpenedClosingClosedFaulted,随着系统的运行状态会改变,

(二)、WCF通道模型

服务端通道代码:

using System; using System.ServiceModel; using System.ServiceModel.Channels; namespace WCF.Second { class Input { static void Main(string[] args) { try { //建立和发送端相同的通道栈 BindingElement[] bindingElements = new BindingElement[2]; bindingElements[0] = new TextMessageEncodingBindingElement(); bindingElements[1] = new HttpTransportBindingElement(); CustomBinding binding = new CustomBinding(bindingElements); //建立ChannelListner IChannelListener<IReplyChannel> listener = binding.BuildChannelListener<IReplyChannel>(new Uri("http://localhost:9090/RequestReplyService"), new BindingParameterCollection()); listener.Open(); //创建IReplyChannel IReplyChannel replyChannel = listener.AcceptChannel(); replyChannel.Open(); Console.WriteLine("开始接受消息。。。"); //接受并打印消息 RequestContext requestContext = replyChannel.ReceiveRequest(); Console.WriteLine("接受到一条消息,action为:{0},body为:{1}", requestContext.RequestMessage.Headers.Action, requestContext.RequestMessage.GetBody<String>()); //创建返回消息 Message response = Message.CreateMessage(binding.MessageVersion, "response", "reponse body"); //发送返回消息 requestContext.Reply(response); //关闭 requestContext.Close(); replyChannel.Close(); listener.Close(); Console.Read(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { Console.Read(); } } } }

客户端通道代码:

using System; using System.ServiceModel; using System.ServiceModel.Channels; using System.Xml; using System.Runtime.Serialization; namespace WCF.Second { class Output { //入口方法 static void Main(string[] args) { try { //建立自定义的通道栈 BindingElement[] bindingElements = new BindingElement[2]; bindingElements[0] = new TextMessageEncodingBindingElement(); bindingElements[1] = new HttpTransportBindingElement(); CustomBinding binding = new CustomBinding(bindingElements); using (Message message = Message.CreateMessage(binding.MessageVersion, "sendMessage","Message Body")) { //创建ChannelFactory IChannelFactory<IRequestChannel> factory = binding.BuildChannelFactory<IRequestChannel>(new BindingParameterCollection()); factory.Open(); //这里创建IRequestChannel IRequestChannel requestChannel = factory.CreateChannel(new EndpointAddress("http://localhost:9090/RequestReplyService")); requestChannel.Open(); //发送消息 Message response = requestChannel.Request(message); Console.WriteLine("已经成功发送消息!"); //查看返回消息 Console.WriteLine("接受到一条返回消息,action为:{0},body为:{1}", response.Headers.Action, response.GetBody<String>()); requestChannel.Close(); factory.Close(); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { Console.Read(); } } } }