WCF学习笔记之消息交换模式
在WCF通信中,有三种消息交换模式,OneWay(单向模式), Request/Reponse(请求回复模式), Duplex(双工通信模式)这三种通信方式。下面对这三种消息交换模式进行讲解。
1. OneWay模式,
- [ServiceContract]
- public interface IOneWayService
- {
- [OperationContract(IsOneWay = true)]
- void Add(double x, double y);
- }
需要在OperationContract特性中显示的添加IsOneWay=true来标识这个操作是OneWay的,另外需要注意,此时的方法是不可以带返回值的。如果包含返回值,在启动Service后,可以看到下面截图所示的错误信息,
2. Request/Reponse模式,
当我们定义一个服务的操作时,该操作默认是请求回复模式的。不管该方法是否有返回值。可以通过Fiddler进行监控。
- [ServiceContract]
- public interface IRequestReponseService
- {
- [OperationContract]
- void Add(double x, double y);
- }
通过Fidder发现,当没有返回值时,其实服务给了一个默认的回复消息,
3. Duplex模式
定义双工通信:
- [ServiceContract(CallbackContract = typeof(IDuplexCallbackService))]
- public interface IDuplexService
- {
- [OperationContract(IsOneWay = true)]
- void GetDuplexServiceResult(string username, string password);
- }
- public interface IDuplexCallbackService
- {
- [OperationContract(IsOneWay = true)]
- void ShowDuplexServiceResult(IEnumerable<Department> depts);
- }
在定义服务契约时,同时指定回掉契约。
实现IDuplexService:
- public class DuplexService : IDuplexService
- {
- public void GetDuplexServiceResult(string username, string password)
- {
- if (string.IsNullOrWhiteSpace(username) ||
- string.IsNullOrWhiteSpace(password))
- {
- return;
- }
- if(username == "Yang-Fei" &&
- password == "")
- {
- List<Department> depts = new List<Department>()
- {
- new Department() { Id = , Name="Development" },
- new Department() { Id = , Name="Sales" },
- new Department() { Id = , Name="Operation" },
- };
- IDuplexCallbackService callback =
- OperationContext.Current.GetCallbackChannel<IDuplexCallbackService>();
- callback.ShowDuplexServiceResult(depts);
- }
- }
- }
我们可以使用netTcpBinding/wsDualHttpBinding来作为双工通信的binding。
App.config:
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <startup>
- <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
- </startup>
- <system.serviceModel>
- <services>
- <service name="Services.DuplexService" behaviorConfiguration="DuplexServiceBehavior">
- <endpoint address="" binding="netTcpBinding" contract="Services.IDuplexService">
- <identity>
- <dns value="localhost"/>
- </identity>
- </endpoint>
- <endpoint address="mex" binding="netTcpBinding" contract="IMetadataExchange"/>
- <host>
- <baseAddresses>
- <add baseAddress="net.tcp://10.12.65.145/DuplexService"/>
- </baseAddresses>
- </host>
- </service>
- </services>
- <behaviors>
- <serviceBehaviors>
- <behavior name="DuplexServiceBehavior">
- <serviceMetadata httpGetEnabled="false"/>
- <serviceDebug includeExceptionDetailInFaults="true"/>
- </behavior>
- </serviceBehaviors>
- </behaviors>
- </system.serviceModel>
- </configuration>
需要在客户端实现回掉服务,例如:
- public class DuplexCallbackServiceClient : IDuplexServiceCallback
- {
- public void ShowDuplexServiceResult(Department[] depts)
- {
- foreach (Department dept in depts)
- {
- Console.WriteLine("Dept Id: {0} Dept Name: {1}", dept.Id, dept.Name);
- }
- }
- }
对服务进行调用:
- static void Main(string[] args)
- {
- DuplexCallbackServiceClient client = new DuplexCallbackServiceClient();
- InstanceContext context = new InstanceContext(client);
- using (DuplexServiceClient proxy = new DuplexServiceClient(context))
- {
- string username = "Yang-Fei";
- string password = "";
- proxy.GetDuplexServiceResult(username, password);
- }
- Console.ReadKey();
- }
运行结果:
代码点击这里下载,
感谢您的阅读。
WCF学习笔记之消息交换模式的更多相关文章
- WCF系列教程之消息交换模式之请求与答复模式(Request/Reply)
1.使用WCF请求与答复模式须知 (1).客户端调用WCF服务端需要等待服务端的返回,即使返回类型是void (2).相比Duplex来讲,这种模式强调的是客户端的被动接受,也就是说客户端接受到响应后 ...
- WCF消息交换模式之双工通讯(Duplex)
WCF消息交换模式之双工通讯(Duplex) 双工通讯Duplex具有以下特点: 1它可以在处理完请求之后,通过请求客户端中的回调进行响应操作 2.消息交换过程中,服务端和客户端角色会发生调换 3.服 ...
- WCF初探-3:WCF消息交换模式之单向模式
单向模式(One-Way Calls): 在这种交换模式中,存在着如下的特征: 只有客户端发起请求,服务端并不会对请求进行回复 不能包含ref或者out类型的参数 没有返回值,返回类型只能为void ...
- WCF初探-4:WCF消息交换模式之请求与答复模式
请求与答复模式( Request/Reply) 这种交换模式是使用最多的一中,它有如下特征: 调用服务方法后需要等待服务的消息返回,即便该方法返回 void 类型 相比Duplex来讲,这种模式强调的 ...
- WCF消息交换模式之请求-响应模式
WCF的消息交换模式(MEP)有三种:请求/响应.单向模式和双工模式.WCF的默认MEP是请求/响应模式. 请求/响应模式操作签名代码如下,无需指定模式,默认就是. [OperationContrac ...
- WCF系列教程之WCF消息交换模式之单项模式
1.使用WCF单项模式须知 (1).WCF服务端接受客户端的请求,但是不会对客户端进行回复 (2).使用单项模式的服务端接口,不能包含ref或者out类型的参数,至于为什么,请参考C# ref与out ...
- WCF学习之旅—HTTP双工模式(二十)
WCF学习之旅—请求与答复模式和单向模式(十九) 四.HTTP双工模式 双工模式建立在上文所实现的两种模式的基础之上,实现客户端与服务端相互调用:前面介绍的两种方法只是在客户端调用服务端的方法,然后服 ...
- WCF学习笔记之事务编程
WCF学习笔记之事务编程 一:WCF事务设置 事务提供一种机制将一个活动涉及的所有操作纳入到一个不可分割的执行单元: WCF通过System.ServiceModel.TransactionFlowA ...
- WCF学习笔记之传输安全
WCF学习笔记之传输安全 最近学习[WCF全面解析]下册的知识,针对传输安全的内容做一个简单的记录,这边只是简单的记录一些要点:本文的内容均来自[WCF全面解析]下册: WCF的传输安全主要涉及认证. ...
随机推荐
- ubuntu dhcp修改ip地址
sudo vim /var/lib/dhcp/dhclient.eth0.leases 把里边的fixed-address都改成你想要的ip. 然后执行 sudo ifdown eth0 && ...
- android SDK manager 无法获取更新版本列表
打开SDK Manager---Tools---Options,填入如下代理和端口,勾选选项也如下. 网址:mirrors.neusoft.edu.cn 端口:80 99%是成功的 参考:http:/ ...
- SpringMVC中Controller跳转到另一个Controller方法
1.直接Redirect后加 Controller/Action Response.Redirect("/User/Edit"); return Redirect("/U ...
- centos vim配置高亮语法和格式化粘贴
centos vim配置高亮语法和格式化粘贴 设置vim别名和高亮grep词语 echo -e "\nalias vi=vim\nalias grep='grep --color'\n&qu ...
- Unity3d 鼠标拣选小功能集合
最近在做一些优化工具,把鼠标拣选的功能单独抽出来. 可遍历所有选中的某类型资源,会递归文件夹 可编译所有prefab的某个Component,也是递归的 using UnityEngine; usin ...
- 和我一起学python,控制语句 (life is short ,we need python)
控制语句 if/elif/else if语句和一般编程语言一样,条件为true 执行 如: if true : print 'true' <----if.else下对齐,要使用相 ...
- C Primer Plus_第二章_C语言概述_复习题与编程练习
REVIEW 1.如何称呼C程序的基本模块? ans 它们被称为函数 2.什么是语法错误?给出一个英语例子和一个C语言例子 me C的语法错误是指把正确的C符号放在了错误的位置 likes codin ...
- 【linux】英文显示乱码解决
在linux环境中中文显示正常,而英文却显示乱码 用 echo $LANG 显示编码为 zh_CN.GB18030 解决方法: 输入 export LC_ALL=POSIX 即可
- 51nod 1449 砝码称重(贪心算法)
题目:传送门. 题意:中文题. 题解:左物右码,w进制.m%w==0||m%w==1||m%w==w-1都是可以的,否则是NO. #include <iostream> #include ...
- UliPad双击没反应,UliPad打不开
关于这个问题呢我也是蛋疼了好久,前几天是把这东西卸了重装,然后莫名其妙就可以了. 今天又遇到这问题,第一个想到的也是重装,发现不行,于是就搜了下,发现果然是网能的网友,下面贴图: 经过本屌几次尝试,鉴 ...