国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > php教程 > WCF 之 通过绑定进行消息通信

WCF 之 通过绑定进行消息通信

来源:程序员人生   发布时间:2015-05-25 09:14:25 阅读次数:2401次
        WCF可以分成两个部份:服务模型层(Service Model Layer)和信道层(Channel Layer).服务模型层建立在信道层之上,提供了1个统1的、可扩大的编程模型。信道层则通过绑定创建的信道栈为消息通讯提供了1个传输、处理的通道。


绑定与信道栈(Binding and Channel Stack)

        绑定,在WCF全部结构体系中扮演着中间人的角色。当服务被成功寄宿时,WCF通过终结点的绑定对象创建1个或多个信道监听器(ChannelListener),绑定到监听端口进行要求的侦听。当要求消息抵达,则利用信道监听器创建的信道栈进行消息的接收。服务操作履行的结果终究封装到回复消息中,通过相同的信道栈被回送。在客户端,通过绑定创建信道工厂(ChannelFactory),借助信道工厂创建的信道栈进行要求消息的发送与回复消息的接收。


下面我们就具体来看1个通过绑定进行消息通讯的实例

1、创建全部解决方案

    MessageViaBinding.Listener:1个控制台利用程序,摹拟消息的监听方。

    MessageViaBinding.Sender:1个控制台利用程序,摹拟消息的发送方。


                                                    


2、创建监听端利用程序

using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.ServiceModel.Channels; using System.Text; using System.Runtime.Serialization; namespace MessageViaBinding.Listener { class Program { static void Main(string[] args) { Uri listenUri = new Uri("http://localhost:8008/listener"); //创建BasicHttpBinding对象 Binding binding = new BasicHttpBinding(); //创建信道监听器对象,listenUri为监听地址 IChannelListener<IReplyChannel> channelListener = binding.BuildChannelListener<IReplyChannel>(listenUri); //打开信道监听器对象 channelListener.Open(); //创建信道栈进行要求的监听 IReplyChannel channel = channelListener.AcceptChannel(TimeSpan.MaxValue); channel.Open(); Console.WriteLine("开始监听..."); while (true) { RequestContext requestContext = channel.ReceiveRequest(TimeSpan.MaxValue); Console.WriteLine("接收到要求消息: {0}", requestContext.RequestMessage); requestContext.Reply(CreateReplyMessage(binding)); } } //创建回复消息 static Message CreateReplyMessage(Binding binding) { string action = "urn:artech.com/reply"; string body = "这是1个简单的回复消息!"; return Message.CreateMessage(binding.MessageVersion, action, body); } } }


3、创建发送端利用程序

using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.ServiceModel.Channels; using System.Text; namespace MessageViaBinding.Sender { class Program { static void Main(string[] args) { Uri listenUri = new Uri("http://localhost:8008/listener"); //创建BasicHttpBinding对象 Binding binding = new BasicHttpBinding(); IChannelFactory<IRequestChannel> channelFactory = binding.BuildChannelFactory<IRequestChannel>(); channelFactory.Open(); IRequestChannel channel = channelFactory.CreateChannel(new EndpointAddress(listenUri)); channel.Open(); Message replyMessage = channel.Request(CreateRequestMessage(binding)); Console.WriteLine ("接收到回复消息 {0}",replyMessage); Console.Read(); } //创建要求消息 static Message CreateRequestMessage(Binding binding) { string action = "urn:artech.com/request"; string body = "这是1个简单的要求消息!"; return Message.CreateMessage(binding.MessageVersion, action, body); } } }


启动监听端

                 

启动发送端发送要求消息,监听端就可以马上监听到该要求消息

                  

同时,监听端发送回复消息,发送端接收到该回复消息

                  






生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生