在WCF通信中,有三种消息交换模式,OneWay(单向模式), Request/Reponse(请求回复模式), Duplex(双工通信模式)这三种通信方式。下面对这三种消息交换模式进行讲解。

1. OneWay模式,

  1. [ServiceContract]
  2. public interface IOneWayService
  3. {
  4. [OperationContract(IsOneWay = true)]
  5. void Add(double x, double y);
  6. }

需要在OperationContract特性中显示的添加IsOneWay=true来标识这个操作是OneWay的,另外需要注意,此时的方法是不可以带返回值的。如果包含返回值,在启动Service后,可以看到下面截图所示的错误信息,

2. Request/Reponse模式,

当我们定义一个服务的操作时,该操作默认是请求回复模式的。不管该方法是否有返回值。可以通过Fiddler进行监控。

  1. [ServiceContract]
  2. public interface IRequestReponseService
  3. {
  4. [OperationContract]
  5. void Add(double x, double y);
  6. }

通过Fidder发现,当没有返回值时,其实服务给了一个默认的回复消息,

3. Duplex模式

定义双工通信:

  1. [ServiceContract(CallbackContract = typeof(IDuplexCallbackService))]
  2. public interface IDuplexService
  3. {
  4. [OperationContract(IsOneWay = true)]
  5. void GetDuplexServiceResult(string username, string password);
  6. }
  1. public interface IDuplexCallbackService
  2. {
  3. [OperationContract(IsOneWay = true)]
  4. void ShowDuplexServiceResult(IEnumerable<Department> depts);
  5. }

在定义服务契约时,同时指定回掉契约。

实现IDuplexService:

  1. public class DuplexService : IDuplexService
  2. {
  3. public void GetDuplexServiceResult(string username, string password)
  4. {
  5. if (string.IsNullOrWhiteSpace(username) ||
  6. string.IsNullOrWhiteSpace(password))
  7. {
  8. return;
  9. }
  10.  
  11. if(username == "Yang-Fei" &&
  12. password == "")
  13. {
  14. List<Department> depts = new List<Department>()
  15. {
  16. new Department() { Id = , Name="Development" },
  17. new Department() { Id = , Name="Sales" },
  18. new Department() { Id = , Name="Operation" },
  19. };
  20.  
  21. IDuplexCallbackService callback =
  22. OperationContext.Current.GetCallbackChannel<IDuplexCallbackService>();
  23.  
  24. callback.ShowDuplexServiceResult(depts);
  25. }
  26. }
  27. }

我们可以使用netTcpBinding/wsDualHttpBinding来作为双工通信的binding。

App.config:

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3. <startup>
  4. <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  5. </startup>
  6. <system.serviceModel>
  7. <services>
  8. <service name="Services.DuplexService" behaviorConfiguration="DuplexServiceBehavior">
  9. <endpoint address="" binding="netTcpBinding" contract="Services.IDuplexService">
  10. <identity>
  11. <dns value="localhost"/>
  12. </identity>
  13. </endpoint>
  14. <endpoint address="mex" binding="netTcpBinding" contract="IMetadataExchange"/>
  15. <host>
  16. <baseAddresses>
  17. <add baseAddress="net.tcp://10.12.65.145/DuplexService"/>
  18. </baseAddresses>
  19. </host>
  20. </service>
  21. </services>
  22. <behaviors>
  23. <serviceBehaviors>
  24. <behavior name="DuplexServiceBehavior">
  25. <serviceMetadata httpGetEnabled="false"/>
  26. <serviceDebug includeExceptionDetailInFaults="true"/>
  27. </behavior>
  28. </serviceBehaviors>
  29. </behaviors>
  30. </system.serviceModel>
  31. </configuration>

需要在客户端实现回掉服务,例如:

  1. public class DuplexCallbackServiceClient : IDuplexServiceCallback
  2. {
  3. public void ShowDuplexServiceResult(Department[] depts)
  4. {
  5. foreach (Department dept in depts)
  6. {
  7. Console.WriteLine("Dept Id: {0} Dept Name: {1}", dept.Id, dept.Name);
  8. }
  9. }
  10. }

对服务进行调用:

  1. static void Main(string[] args)
  2. {
  3. DuplexCallbackServiceClient client = new DuplexCallbackServiceClient();
  4.  
  5. InstanceContext context = new InstanceContext(client);
  6.  
  7. using (DuplexServiceClient proxy = new DuplexServiceClient(context))
  8. {
  9. string username = "Yang-Fei";
  10.  
  11. string password = "";
  12.  
  13. proxy.GetDuplexServiceResult(username, password);
  14. }
  15.  
  16. Console.ReadKey();
  17. }

运行结果:

代码点击这里下载,

感谢您的阅读。

WCF学习笔记之消息交换模式的更多相关文章

  1. WCF系列教程之消息交换模式之请求与答复模式(Request/Reply)

    1.使用WCF请求与答复模式须知 (1).客户端调用WCF服务端需要等待服务端的返回,即使返回类型是void (2).相比Duplex来讲,这种模式强调的是客户端的被动接受,也就是说客户端接受到响应后 ...

  2. WCF消息交换模式之双工通讯(Duplex)

    WCF消息交换模式之双工通讯(Duplex) 双工通讯Duplex具有以下特点: 1它可以在处理完请求之后,通过请求客户端中的回调进行响应操作 2.消息交换过程中,服务端和客户端角色会发生调换 3.服 ...

  3. WCF初探-3:WCF消息交换模式之单向模式

    单向模式(One-Way Calls): 在这种交换模式中,存在着如下的特征: 只有客户端发起请求,服务端并不会对请求进行回复 不能包含ref或者out类型的参数 没有返回值,返回类型只能为void ...

  4. WCF初探-4:WCF消息交换模式之请求与答复模式

    请求与答复模式( Request/Reply) 这种交换模式是使用最多的一中,它有如下特征: 调用服务方法后需要等待服务的消息返回,即便该方法返回 void 类型 相比Duplex来讲,这种模式强调的 ...

  5. WCF消息交换模式之请求-响应模式

    WCF的消息交换模式(MEP)有三种:请求/响应.单向模式和双工模式.WCF的默认MEP是请求/响应模式. 请求/响应模式操作签名代码如下,无需指定模式,默认就是. [OperationContrac ...

  6. WCF系列教程之WCF消息交换模式之单项模式

    1.使用WCF单项模式须知 (1).WCF服务端接受客户端的请求,但是不会对客户端进行回复 (2).使用单项模式的服务端接口,不能包含ref或者out类型的参数,至于为什么,请参考C# ref与out ...

  7. WCF学习之旅—HTTP双工模式(二十)

    WCF学习之旅—请求与答复模式和单向模式(十九) 四.HTTP双工模式 双工模式建立在上文所实现的两种模式的基础之上,实现客户端与服务端相互调用:前面介绍的两种方法只是在客户端调用服务端的方法,然后服 ...

  8. WCF学习笔记之事务编程

    WCF学习笔记之事务编程 一:WCF事务设置 事务提供一种机制将一个活动涉及的所有操作纳入到一个不可分割的执行单元: WCF通过System.ServiceModel.TransactionFlowA ...

  9. WCF学习笔记之传输安全

    WCF学习笔记之传输安全 最近学习[WCF全面解析]下册的知识,针对传输安全的内容做一个简单的记录,这边只是简单的记录一些要点:本文的内容均来自[WCF全面解析]下册: WCF的传输安全主要涉及认证. ...

随机推荐

  1. ubuntu dhcp修改ip地址

    sudo vim /var/lib/dhcp/dhclient.eth0.leases 把里边的fixed-address都改成你想要的ip. 然后执行 sudo ifdown eth0 && ...

  2. android SDK manager 无法获取更新版本列表

    打开SDK Manager---Tools---Options,填入如下代理和端口,勾选选项也如下. 网址:mirrors.neusoft.edu.cn 端口:80 99%是成功的 参考:http:/ ...

  3. SpringMVC中Controller跳转到另一个Controller方法

    1.直接Redirect后加 Controller/Action Response.Redirect("/User/Edit"); return Redirect("/U ...

  4. centos vim配置高亮语法和格式化粘贴

    centos vim配置高亮语法和格式化粘贴 设置vim别名和高亮grep词语 echo -e "\nalias vi=vim\nalias grep='grep --color'\n&qu ...

  5. Unity3d 鼠标拣选小功能集合

    最近在做一些优化工具,把鼠标拣选的功能单独抽出来. 可遍历所有选中的某类型资源,会递归文件夹 可编译所有prefab的某个Component,也是递归的 using UnityEngine; usin ...

  6. 和我一起学python,控制语句 (life is short ,we need python)

    控制语句 if/elif/else if语句和一般编程语言一样,条件为true 执行 如: if true : print 'true'         <----if.else下对齐,要使用相 ...

  7. C Primer Plus_第二章_C语言概述_复习题与编程练习

    REVIEW 1.如何称呼C程序的基本模块? ans 它们被称为函数 2.什么是语法错误?给出一个英语例子和一个C语言例子 me C的语法错误是指把正确的C符号放在了错误的位置 likes codin ...

  8. 【linux】英文显示乱码解决

    在linux环境中中文显示正常,而英文却显示乱码 用 echo $LANG 显示编码为 zh_CN.GB18030 解决方法: 输入 export LC_ALL=POSIX 即可

  9. 51nod 1449 砝码称重(贪心算法)

    题目:传送门. 题意:中文题. 题解:左物右码,w进制.m%w==0||m%w==1||m%w==w-1都是可以的,否则是NO. #include <iostream> #include ...

  10. UliPad双击没反应,UliPad打不开

    关于这个问题呢我也是蛋疼了好久,前几天是把这东西卸了重装,然后莫名其妙就可以了. 今天又遇到这问题,第一个想到的也是重装,发现不行,于是就搜了下,发现果然是网能的网友,下面贴图: 经过本屌几次尝试,鉴 ...