WCF 之 通过绑定进行消息通信
2015-04-30 11:04
363 查看
WCF可以分成两个部分:服务模型层(Service Model Layer)和信道层(Channel Layer).服务模型层建立在信道层之上,提供了一个统一的、可扩展的编程模型。信道层则通过绑定创建的信道栈为消息通信提供了一个传输、处理的通道。
绑定与信道栈(Binding and Channel Stack)
绑定,在WCF整个结构体系中扮演着中间人的角色。当服务被成功寄宿时,WCF通过终结点的绑定对象创建一个或多个信道监听器(ChannelListener),绑定到监听端口进行请求的侦听。当请求消息抵达,则利用信道监听器创建的信道栈进行消息的接收。服务操作执行的结果最终封装到回复消息中,通过相同的信道栈被回送。在客户端,通过绑定创建信道工厂(ChannelFactory),借助信道工厂创建的信道栈进行请求消息的发送与回复消息的接收。
下面我们就具体来看一个通过绑定进行消息通信的实例
一、创建整个解决方案
MessageViaBinding.Listener:一个控制台应用程序,模拟消息的监听方。
MessageViaBinding.Sender:一个控制台应用程序,模拟消息的发送方。
二、创建监听端应用程序
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("接收到请求消息:\n{0}", requestContext.RequestMessage);
requestContext.Reply(CreateReplyMessage(binding));
}
}
//创建回复消息
static Message CreateReplyMessage(Binding binding)
{
string action = "urn:artech.com/reply";
string body = "这是一个简单的回复消息!";
return Message.CreateMessage(binding.MessageVersion, action, body);
}
}
}
三、创建发送端应用程序
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 ("接收到回复消息\n{0}",replyMessage);
Console.Read();
}
//创建请求消息
static Message CreateRequestMessage(Binding binding)
{
string action = "urn:artech.com/request";
string body = "这是一个简单的请求消息!";
return Message.CreateMessage(binding.MessageVersion, action, body);
}
}
}
启动监听端
启动发送端发送请求消息,监听端就能马上监听到该请求消息
同时,监听端发送回复消息,发送端接收到该回复消息
绑定与信道栈(Binding and Channel Stack)
绑定,在WCF整个结构体系中扮演着中间人的角色。当服务被成功寄宿时,WCF通过终结点的绑定对象创建一个或多个信道监听器(ChannelListener),绑定到监听端口进行请求的侦听。当请求消息抵达,则利用信道监听器创建的信道栈进行消息的接收。服务操作执行的结果最终封装到回复消息中,通过相同的信道栈被回送。在客户端,通过绑定创建信道工厂(ChannelFactory),借助信道工厂创建的信道栈进行请求消息的发送与回复消息的接收。
下面我们就具体来看一个通过绑定进行消息通信的实例
一、创建整个解决方案
MessageViaBinding.Listener:一个控制台应用程序,模拟消息的监听方。
MessageViaBinding.Sender:一个控制台应用程序,模拟消息的发送方。
二、创建监听端应用程序
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("接收到请求消息:\n{0}", requestContext.RequestMessage);
requestContext.Reply(CreateReplyMessage(binding));
}
}
//创建回复消息
static Message CreateReplyMessage(Binding binding)
{
string action = "urn:artech.com/reply";
string body = "这是一个简单的回复消息!";
return Message.CreateMessage(binding.MessageVersion, action, body);
}
}
}
三、创建发送端应用程序
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 ("接收到回复消息\n{0}",replyMessage);
Console.Read();
}
//创建请求消息
static Message CreateRequestMessage(Binding binding)
{
string action = "urn:artech.com/request";
string body = "这是一个简单的请求消息!";
return Message.CreateMessage(binding.MessageVersion, action, body);
}
}
}
启动监听端
启动发送端发送请求消息,监听端就能马上监听到该请求消息
同时,监听端发送回复消息,发送端接收到该回复消息
相关文章推荐
- [原创]WCF后续之旅(17):通过tcpTracer进行消息的路由
- wcf通过binding进行通信
- 通过拦截WCF消息进行身份栈传播
- mfc通过消息传递参数进行程序间通信
- [WCF安全系列]通过绑定元素看各种绑定对消息保护的实现
- WCF后续之旅(17):通过tcpTracer进行消息的路由
- WCF之旅读书笔记(2):WCF如何通过Binding进行通信
- WCF后续之旅(1): WCF是如何通过Binding进行通信的
- [原创]WCF后续之旅(1): WCF是如何通过Binding进行通信的
- WCF 第四章 绑定 使用队列技术进行通信
- WCF后续之旅(1): WCF是如何通过Binding进行通信的
- WCF服务通过TCP实时监控客户端状态,并可以向客户端广播推送消息,实现双向通信
- WCF后续之旅(1): WCF是如何通过Binding进行通信的
- 进程间通信一:通过消息进行通信(WM_COPYDATA)
- WCF 第四章 绑定 使用队列技术进行通信
- C#与Java通过protobuf进行网络通信过程中遇到的问题
- 通过父子进程实现消息队列的互相通信
- Service 通过 BroadcastReceiver与Activity 进行通信
- 稳扎稳打Silverlight(53) - 4.0通信之对WCF NetTcpBinding的支持, 在Socket通信中通过HTTP检索策略文件, HTTP请求中的ClientHttp和BrowserHttp
- 【WCF安全】SOAP消息实现用户名验证:通过OperationContext直接添加/访问MessageHeader信息