WCF 第三章 信道形状

WCF支持不同的消息交换模式:单向,双工和请求-回复。为了实现每种方式,WCF 提供了10种不同的称作信道形状的接口。其中五个形状称作IOutputChannel, IInputChannel, IDuplexChannel, IRequestChannel和IReplyChannel.每个形状都有一个等效的支持会话的形状。它们包括 IOutputSessionChannel, IInputSessionChannel, IDuplexSessionChannel, IRequestSessionChannel和IReplySessionChannel.这些接口在一个信道栈中实现了不同的消息交换模式。在这一部 分,我们将查看每一个通信模式以及与它们关联的多种接口。
单向通信模式
在单向通信模式中,消息仅被发送到唯一的地方,从客户度到服务端。当发送方当前不需要反馈消息时,单向通信很常用;发送方仅仅需要一个消息已经 被发送的确认。消息的发送,就是消息交换的终点。用来实现单向通信的两个接口是IOutputChannel和IInputChannel.图片3.2 显示了在消息如何在客户端和服务端的单向通信中流转。
在这个模式中,IOutputChannel接口用来发送消息,IInputChannel用来接收消息。列表3.1 显示了使用IOutputChannel信道类型发送消息的客户端应用程序。
列表3.1 IOutputChannel 例子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace EssentialWCF
{
class ChannelShape
{
static void Main(string[] args)
{
BasicHttpBinding binding
= new BasicHttpBinding();
BindingParameterCollection parameters
= new BindingParameterCollection();
Message m
= Message.CreateMessage(MessageVersion.Soap11, "urn:sendmessage");
IChannelFactory
<IOutputChannel> factory =
binding.BuildChannelFactory<IOutputChannel>(parameters);
IOutputChannel channel
=
factory.CreateChannel(new EndpointAddress("http://localhost/sendmessage/"));
channel.Send(m);
channel.Close();
factory.Close();
}
}
}
 
双工通信使用两个单向信道形状,并附加另外一个称为IDuplexChannel的信道形状,如图片3.3所示。双工通信相比单向通信的优势在于消息可以从客户端或者服务端发出。
双工通信的例子是事件提醒系统。服务端将向事件接收端发送事件。客户端提供一个服务端用来发送消息的终结点。列表3.2显示了使用IDuplexChannel信道形状通信的客户端。
列表3.2 IDuplexChannel例子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace EssentialWCF
{
class ChannelShape
{
static void Main(string[] args)
{
NetTcpBinding binding
= new NetTcpBinding();
BindingParameterCollection parameters
= new BindingParameterCollection();
Message m
= Message.CreateMessage(MessageVersion.Soap12WSAddressing10, "urn:sendmessage");
IChannelFactory
<IDuplexChannel> factory =
binding.BuildChannelFactory<IDuplexChannel>(parameters);
IOutputChannel channel
=
factory.CreateChannel(new EndpointAddress("net.tcp://localhost/sendmessage/"));
channel.Send(m);
channel.Close();
factory.Close();
}
}
}
 
请求-回复通信
请求-回复通信是双向通信的特殊形式,它的每个请求只有一个回复,而且它总是在客户端初始化。在客户端发送一个请求后,它必须在发送另外一个请求前等待一个回复。
最常见的请求-回复通信是浏览器的HTTP请求。浏览器向服务器发送一个HTTP请求,比如GET或者POST,服务器处理请求,然后发送一个回复。WCF使用如图3.4中显示的IRequestChannel和IReplyChannel接口来处理请求-回复通信。
列表3.3 显示了一个使用IRequestChannel接口来发送消息的客户端应用程序。注意Request方法的返回参数是Reply消息。
列表3.3 IRequestChannel例子
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace EssentialWCF
{
class ChannelShape
{
static void Main(string[] args)
{
BasicHttpBinding binding
= new BasicHttpBinding();
BindingParameterCollection parameters
= new BindingParameterCollection();
Message request
= Message.CreateMessage(MessageVersion.Soap11, "urn:sendmessage");
IChannelFactory
<IRequestChannel> factory =
binding.BuildChannelFactory<IRequestChannel>(parameters);
IRequestChannel channel
=
actory.CreateChannel(new EndpointAddress("http://localhost/sendmessage/"));
Message response
= channel.Request(request);
channel.Close();
factory.Close();
}
}
}
 
形状改变
在HTTP协议中有一个固有的请求-回复特性,因此HTTP传输信道使用请求-回复信道形状。其他形式的通信,比如单向和在HTTP协议上的双 工通信,都是通过形状改变实现的。它是通过在传输信道上层加一层协议信道来支持单向或者双工通信。列表3.4 显示了一个在一个HTTP传输信道上添加一个单向形状改变绑定元素OneWayBindingElement的自定义绑定。我们将会在第十二章”对等网 络”中看到更多使用CompositeDuplexBindingElement绑定元素的信道改变的高级例子。
列表3.4 IRequestChannel例子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace EssentialWCF
{
class ChannelShape
{
static void Main(string[] args)
{
CustomBinding binding
= new CustomBinding(
new OneWayBindingElement(),
new TextMessageEncodingBindingElement(),
new HttpsTransportBindingElement());
}
}
}

===========

转载自

 

posted @ 2011-06-29 10:06  Gavin Liu  阅读(200)  评论(0编辑  收藏  举报

Right people get the right information at the right time.
以技术求生存,以市场求发展;学以至用,开拓创新;达技术之颠峰,至市场之广阔!