建筑公司网站首页,wordpress qq 微信登录,网站设计内容包括,网站开发技术的现状及发展趋势一、概述 WCF在通信过程中有三种模式#xff1a;请求与答复、单向、双工通信。以下我们一一介绍。 二、请求与答复模式 描述#xff1a; 客户端发送请求#xff0c;然后一直等待服务端的响应(异步调用除外)#xff0c;期间处于假死状态#xff0c;直到服务端有了答复后才能…一、概述 WCF在通信过程中有三种模式请求与答复、单向、双工通信。以下我们一一介绍。 二、请求与答复模式 描述 客户端发送请求然后一直等待服务端的响应(异步调用除外)期间处于假死状态直到服务端有了答复后才能继续执行其他程序如下图所示(图中的粗红线在此时代表顺序并不代表调用) 请求与答复模式为WCF的默认模式如下代码所示 1 [OperationContract]
2 string ShowName(string name); 即使返回值是void 也属于请求与答复模式。 缺点如果用WCF在程序A中上传一个2G的文件那么要想执行程序B也许就是几个小时后的事情了。如果操作需要很长的时间那么客户端程序的响应能力将会大大的下降。 优点有返回值我们就可以向客户端返回错误信息如只接收.rar文件等信息。 实例 1 //服务端接口2 using System.ServiceModel;3 4 namespace WCFService_Default5 {6 [ServiceContract]7 public interface IUser8 {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 string ShowName(string name); 优缺点与“请求响应模式”差不多倒过来。 特点使用 IsOneWaytrue 标记的操作不得声明输出参数、引用参数或返回值 实例 1 //服务端接口2 using System.ServiceModel;3 4 namespace WCFService_OneWay5 {6 [ServiceContract]7 public interface IUser8 {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 IUser3 {4 [OperationContract]5 string ShowName(string name);6 }7 //回调的接口 8 public interface IUserCallback9 {
10 [OperationContract(IsOneWay true)]
11 void PrintSomething(string str);
12 } 实例 支持回调的绑定有4种WSDualHttpBinding、NetTcpBinding、NetNamedPipeBinding、NetPeerTcpBinding。我们这里用WSDualHttpBinding为例 1 //配置文件中的 binding 指定2 endpoint address bindingwsDualHttpBinding contractWCFService_DualPlex.IUser/endpoint3 4 //服务端接口5 using System.ServiceModel;6 7 namespace WCFService_DualPlex8 {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.GetCallbackChannelIUserCallback();
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 教程转载于:https://www.cnblogs.com/wujy/archive/2013/04/08/3007385.html