WCF异步调用正确实现方法讲解

开发 开发工具
WCF异步调用议案都是基于消息交换来进行实现的。我们在这篇我文章中将会通过这一操作方法进行详细的介绍方便大家了解。

WCF编程工具是.NET Framework 3.5的重要组成部分,它独特的统一性,安全性以及兼容性可以帮助开发人员创造一个功能强大的企业级应用解决方案。其中,WCF异步调用是基于消息交换(Message Exchange)来实现的,和我们通常使用委托来实现异步调用有所不同。

WCF异步调用步骤1. 创建服务契约。

  1. [ServiceContract(SessionModeSessionMode=SessionMode.Required)]  
  2. public interface ICalculate  
  3. {  
  4. [OperationContract]  
  5. int Add(int a, int b);  

WCF异步调用步骤2. 为契约方法添加异步版本。

我们为 Add 方法添加了 BeginAdd 和 EndAdd 两个在 .NET SDK 中 "常见" 的异步操作方法。注意 BeginAdd 方法中我们添加了异步声明,而 EndAdd 方法没有。还有就是要注意异步版本方法的参数。

  1. [ServiceContract(SessionModeSessionMode=SessionMode.Required)]  
  2. public interface ICalculate  
  3. {  
  4. [OperationContract]  
  5. int Add(int a, int b);  
  6. [OperationContract(AsyncPattern=true)]  
  7. IAsyncResult BeginAdd(int a, int b, AsyncCallback callBack, 
    object state);  
  8. int EndAdd(IAsyncResult ar);  

WCF异步调用步骤3. 实现服务契约。

你可能注意到了,我们并没有创建 Add 的委托原型,也没有 "真正" 实现 BeginAdd 和 EndAdd。这是因为消息交换会 "异步" 调用 Add 方法,所有的异步版本方法只是用来创建消息声明而已。

  1. public class CalculateService : ICalculate  
  2. {  
  3. public int Add(int a, int b)  
  4. {  
  5. Thread.Sleep(5000);  
  6. Console.WriteLine(OperationContext.Current.SessionId);  
  7. return a + b;  
  8. }  
  9. public IAsyncResult BeginAdd(int a, int b, AsyncCallback 
    callBack, object state)  
  10. {  
  11. throw new Exception("The method or operation is not implemented.");  
  12. }  
  13. public int EndAdd(IAsyncResult ar)  
  14. {  
  15. throw new Exception("The method or operation is not implemented.");  
  16. }  

WCF异步调用步骤4. 我们给一个完整版本,看看执行结果。

  1. [ServiceContract]  
  2. public interface ICalculate  
  3. {  
  4. [OperationContract]  
  5. int Add(int a, int b);  
  6. [OperationContract(AsyncPattern = true)]  
  7. IAsyncResult BeginAdd(int a, int b, AsyncCallback callBack, 
    object state);  
  8. int EndAdd(IAsyncResult ar);  
  9. }  
  10. public class CalculateService : ICalculate  
  11. {  
  12. public int Add(int a, int b)  
  13. {  
  14. Console.WriteLine("服务器方法 Add 开始执行: {0}", DateTime.Now);  
  15. try  
  16. {  
  17. Thread.Sleep(5000);  
  18. return a + b;  
  19. }  
  20. finally  
  21. {  
  22. Console.WriteLine("服务器方法 Add 执行完成: {0}", DateTime.Now);  
  23. }  
  24. }  
  25. public IAsyncResult BeginAdd(int a, int b, AsyncCallback 
    callBack, object state)  
  26. {  
  27. throw new Exception("The method or operation is not implemented.");  
  28. }  
  29. public int EndAdd(IAsyncResult ar)  
  30. {  
  31. throw new Exception("The method or operation is not implemented.");  
  32. }  
  33. }  
  34. public class WcfTest  
  35. {  
  36. public static void Test()  
  37. {  
  38. AppDomain.CreateDomain("Server").DoCallBack(delegate  
  39. {  
  40. ServiceHost host = new ServiceHost(typeof(CalculateService));  
  41. host.AddServiceEndpoint(typeof(ICalculate), new WSHttpBinding(),  
  42. "http://localhost:8080/calc");  
  43. host.Open();  
  44. });  
  45. ICalculate channel = ChannelFactory<ICalculate>.CreateChannel
    (new WSHttpBinding(),  
  46. new EndpointAddress("http://localhost:8080/calc"));  
  47. using (channel as IDisposable)  
  48. {  
  49. Console.WriteLine("客户端调用 BeginAdd: {0}", DateTime.Now);  
  50. IAsyncResult ar = channel.BeginAdd(1, 2, delegate 
    { Console.WriteLine("CallBack..."); }, null);  
  51. Console.WriteLine("客户端调用 BeginAdd 完成: {0}", DateTime.Now);  
  52. Console.WriteLine(channel.EndAdd(ar));  
  53. Console.WriteLine("客户端调用 EndAdd 完成: {0}", DateTime.Now);  
  54. }  
  55. }  

输出:

  1. 客户端调用 BeginAdd: 2007-4-1 20:56:47  
  2. 客户端调用 BeginAdd 完成: 2007-4-1 20:56:47  
  3. 服务器方法 Add 开始执行: 2007-4-1 20:56:49  
  4. 服务器方法 Add 执行完成: 2007-4-1 20:56:54  
  5. 3  
  6. 客户端调用 EndAdd 完成: 2007-4-1 20:56:55  
  7. CallBack... 

最后在WCF异步调用步骤中需要注意的是,我们必须使用支持 Session 的 Binding 对象 (BasicHttpBinding 会抛出异常)。

【编辑推荐】

  1. WCF集合类型各种限制于规则介绍
  2. WCF集合反序列化具体操作技巧分享
  3. WCF集合数据契约相关定制方法详解
  4. WCF宿主环境基本概念详解
  5. WCF序列化引擎具体应用技巧分享
责任编辑:曹凯 来源: 博客园
相关推荐

2010-03-01 14:01:50

WCF服务异步调用

2010-02-24 13:48:44

MSMQ使用WCF

2009-11-06 15:54:15

WCF异步调用

2009-12-07 14:26:47

WCF异步调用

2009-11-09 10:50:30

WCF异步调用

2009-12-21 14:10:26

WCF异步调用

2010-02-26 11:22:16

LitwareHR使用

2010-02-25 16:52:12

引用WCF服务

2010-02-25 13:48:23

WCF动态创建代码

2010-02-26 10:30:03

ASP.NET Aja

2010-02-25 10:10:29

WCF使用Header

2010-02-24 10:07:48

WCF跨越边界

2010-02-26 08:59:10

WCF服务宿主程序

2009-12-07 14:35:42

WCF异步调用

2010-02-22 13:28:05

WCF异步调用

2010-02-24 10:41:28

WCF服务保护

2009-12-22 18:43:00

WCF异步调用

2010-03-05 16:51:01

Python程序转为E

2009-11-09 15:49:01

WCF异步调用

2010-03-04 11:12:02

Python AOP
点赞
收藏

51CTO技术栈公众号