首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件开发 >

WCF技术黑幕 第3章 (1)

2013-01-20 
WCF技术内幕 第3章 (1)第3章 消息交换模式、拓扑和编排3.1 消息交换模式消息交换模式是“描述消息参与者之间

WCF技术内幕 第3章 (1)

第3章 消息交换模式、拓扑和编排

3.1 消息交换模式

消息交换模式是“描述消息参与者之间交换消息的模板”。

在面向服务的世界里通常有三种类型的消息交换模式:数据报,请求/应答和双工。


数据报交换模式

数据报消息交换模式表示一个单向的消息发送,或者即发即弃的消息发送。如果需要应答数据报,就需要发送者和接收者之间建立一个新的连接来应答数据报。

数据报消息交换模式使用WSDL语言描述为一个包含wsdl:input和没有wsdl:output元素的操作。


数据报和WCF契约:

契约定义方法的返回值类型是void和ContractAttribute属性的IsOneWay属性值为False。
在WCF里,唯一避免回复消息的方法就是在操作属性里设置IsOneWay属性。默认值为false,这个设置让操作默认使用请求/应答消息交换模式。

//另外的一个监听端,监听数据报,只与service的address一样,其他选项没有关系using System;using System.ServiceModel.Channels;namespace ReceiverApp{    class Program    {        static void Main(string[] args)        {            BindingElement[] bindingElements = new BindingElement[3];            bindingElements[0] = new TextMessageEncodingBindingElement();            bindingElements[1] = new OneWayBindingElement();            bindingElements[2] = new HttpTransportBindingElement();            CustomBinding binding = new CustomBinding(bindingElements);            IChannelListener<IInputChannel> listener = binding.BuildChannelListener<IInputChannel>(                new Uri("http://localhost:1234/OneWayContract"),                 new BindingElementCollection());            listener.Open();            IInputChannel inputChannel = listener.AcceptChannel();            inputChannel.Open();            Message message = inputChannel.Receive();            Console.WriteLine(message.Headers.Action); //urn:GetData            message.Close();            inputChannel.Close();            listener.Close();            Console.ReadKey();        }    }}

HTTP和数据报消息交换模式

在WCF里,通过HTTP发送数据报,再通过HTTP发送数据时,回复是一个HTTP202应答代码。HTTP202状态码的定义是:请求已经被处理,但处理还没结束。


热点排行