无废话WCF入门教程五[WCF的通信模式]


朋友炒股两个月赚了10万,我帮他推广一下公众号,把钱用来投资总比放银行连通货膨胀都跑不过里强硬核离职,在家炒股 ,这是他每天的日志,有些经验是花钱也买不到的。

一、概述

  WCF在通信过程中有三种模式:请求与答复、单向、双工通信。以下我们一一介绍。

二、请求与答复模式

  描述:

  客户端发送请求,然后一直等待服务端的响应(异步调用除外),期间处于假死状态,直到服务端有了答复后才能继续执行其他程序,如下图所示(图中的粗红线在此时代表顺序并不代表调用):

  请求与答复模式为WCF的默认模式,如下代码所示:

1  [OperationContract] 2         string ShowName(string name);

  即使返回值是void 也属于请求与答复模式。

  缺点:如果用WCF在程序A中上传一个2G的文件,那么要想执行程序B也许就是几个小时后的事情了。如果操作需要很长的时间,那么客户端程序的响应能力将会大大的下降。

  优点:有返回值我们就可以向客户端返回错误信息,如:只接收".rar"文件等信息。

  实例:

 1 //服务端接口
 2 using System.ServiceModel;  3 
 4 namespace WCFService_Default  5 {  6  [ServiceContract]  7     public interface IUser  8  {  9  [OperationContract] 10         string ShowName(string name); 11  } 12 } 13 //服务端实现
14 namespace WCFService_Default 15 { 16     public class User : IUser 17  { 18         public string ShowName(string name) 19  { 20             //线程睡眠20秒钟
21             System.Threading.Thread.Sleep(20000); 22             return "WCF服务,显示名称:" + name; 23  } 24  } 25 } 26 
27 //客户端调用
28 using System; 29 using WCFClient_Default.WCFService_Default; 30 
31 namespace WCFClient_Default 32 { 33     class Program 34  { 35         static void Main(string[] args) 36  { 37             UserClient client = new UserClient(); 38  Console.WriteLine(DateTime.Now); 39             string result = client.ShowName("李林峰"); 40  Console.WriteLine(result); 41  Console.WriteLine(DateTime.Now); 42  Console.ReadLine(); 43  } 44  } 45 }

  在上例中,我们在服务端让线程睡眠20秒然后再返回客户端,那么客户端两次显示当前时间的间隔必然在20秒以上,如下图所示:

二、单向模式

  描述:

  客户端向服务端发送求,但是不管服务端是否执行完成就接着执行下面的程序。如下图所示:

  单向模式要在OpertaionContract的属性中显示设置值,代码如下:

1         [OperationContract(IsOneWay = true)] 2         void ShowName(string name);

  优缺点与“请求响应模式”差不多倒过来。

  特点:使用 IsOneWay=true 标记的操作不得声明输出参数、引用参数或返回值

  实例:

 1 //服务端接口
 2 using System.ServiceModel;  3 
 4 namespace WCFService_OneWay  5 {  6  [ServiceContract]  7     public interface IUser  8  {  9         [OperationContract(IsOneWay = true)] 10         void DoSomething(); 11  } 12 } 13 
14 //服务端实现
15 namespace WCFService_OneWay 16 { 17     public class User : IUser 18  { 19         public void DoSomething() 20  { 21             //线程睡眠20秒钟
22             System.Threading.Thread.Sleep(20000); 23  } 24  } 25 } 26 
27 //客户端调用
28 using System; 29 using WCFClient_OneWay.WCFService_OneWay; 30 
31 namespace WCFClient_OneWay 32 { 33     class Program 34  { 35         static void Main(string[] args) 36  { 37             UserClient client = new UserClient(); 38  Console.WriteLine(DateTime.Now); 39             //调用WCF服务的方法
40  client.DoSomething(); 41  Console.WriteLine(DateTime.Now); 42  Console.ReadLine(); 43  } 44  } 45 }

  在单向模式中与请求响应模式最主要的就是加IsOneWay属性,运行效果如下:

三、双工模式

  描述:

  双工模式建立在上面两种模式的基础之上,实现客户端与服务端相互的调用。相互调用:以往我们只是在客户端调用服务端,然后服务端有返回值返回客户端,而相互调用不光是客户端调用服务端,而且服务端也可以调用客户端的方法。如下图所示:

  在上图中,客户端的程序A调用服务端的程序A,服务程序A执行完成前又调用客户端的程序D,然后再返回到程序A,图有点乱,其实就是为了说明“服务端”与“客户端”可以相互调用,下面直接看代码。

  如我们所说的,双工模式是建立在以上两种模式之上的模式,他们并不冲突,代码如下:

 1     [ServiceContract(CallbackContract = typeof(IUserCallback))]  2     public interface IUser  3  {  4         [OperationContract]  5         string ShowName(string name);  6  }  7   //回调的接口 
 8     public interface IUserCallback  9  { 10         [OperationContract(IsOneWay = true)] 11         void PrintSomething(string str);
12
}

  实例:

  支持回调的绑定有4种:WSDualHttpBinding、NetTcpBinding、NetNamedPipeBinding、NetPeerTcpBinding。我们这里用WSDualHttpBinding为例

 1 //配置文件中的 binding 指定
 2 <endpoint address=""  binding="wsDualHttpBinding" contract="WCFService_DualPlex.IUser"></endpoint>
 3 
 4 //服务端接口
 5 using System.ServiceModel;  6 
 7 namespace WCFService_DualPlex  8 {  9     [ServiceContract(CallbackContract = typeof(IUserCallback))] 10     public interface IUser 11  { 12  [OperationContract] 13         string ShowName(string name); 14  } 15 
16     public interface IUserCallback 17  { 18         [OperationContract(IsOneWay = true)] 19         void PrintSomething(string str); 20  } 21 } 22 
23 //服务端实现
24 using System.ServiceModel; 25 
26 namespace WCFService_DualPlex 27 { 28     public class User : IUser 29  { 30         IUserCallback callback = null; 31 
32         public User() 33  { 34             callback = OperationContext.Current.GetCallbackChannel<IUserCallback>(); 35  } 36 
37         public string ShowName(string name) 38  { 39             //在服务器端定义字符串,调用客户端的方法向客户端打印
40             string str = "服务器调用客户端..."; 41  callback.PrintSomething(str); 42             //返回服务端方法
43             return "WCF服务,显示名称:" + name; 44  } 45  } 46 } 47 
48 //客户端调用
49 using System; 50 using System.ServiceModel; 51 using WCFClient_DualPlex.WCFService_DualPlex; 52 
53 namespace WCFClient_DualPlex 54 { 55     //实现服务端的回调接口
56     public class CallbackHandler : IUserCallback 57  { 58         public void PrintSomething(string str) 59  { 60  Console.WriteLine(str); 61  } 62  } 63 
64     class Program 65  { 66         static void Main(string[] args) 67  { 68             InstanceContext instanceContext = new InstanceContext(new CallbackHandler()); 69             UserClient client = new UserClient(instanceContext); 70  Console.WriteLine(DateTime.Now); 71             string result = client.ShowName("李林峰"); 72  Console.WriteLine(result); 73  Console.WriteLine(DateTime.Now); 74  Console.ReadLine(); 75  } 76  } 77 }

  在上例中,我们把接口定义在服务端,而实现在客户端,配置文件是由IDE自动生成的,我们在服务端ShowName方法中,调用了PringSomething的方法,实现了服务端向客户端的调用。  

  执行效果如下图所示:

四、代码下载:

  10 WCF 教程

五、版权

  转载请注明出处:http://www.cnblogs.com/iamlilinfeng


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM