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

来源:互联网 发布:成都正规java培训机构 编辑:程序博客网 时间:2024/05/01 03:25

一、概述

  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_Default15 {16     public class User : IUser17     {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_Default32 {33     class Program34     {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_OneWay16 {17     public class User : IUser18     {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_OneWay32 {33     class Program34     {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 IUser11     {12         [OperationContract]13         string ShowName(string name);14     }15 16     public interface IUserCallback17     {18         [OperationContract(IsOneWay = true)]19         void PrintSomething(string str);20     }21 }22 23 //服务端实现24 using System.ServiceModel;25 26 namespace WCFService_DualPlex27 {28     public class User : IUser29     {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_DualPlex54 {55     //实现服务端的回调接口56     public class CallbackHandler : IUserCallback57     {58         public void PrintSomething(string str)59         {60             Console.WriteLine(str);61         }62     }63 64     class Program65     {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

0 0