OperationInvoker_T.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:OperationInvoker_T.cs
  5. * 2.功能描述:服务操作者
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2014/08/13 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.ServiceModel;
  12. using Dongke.IBOSS.PRD.Basics.Library;
  13. namespace Dongke.IBOSS.PRD.WCF.Proxys.ServiceProxy
  14. {
  15. /// <summary>
  16. /// 服务操作者
  17. /// </summary>
  18. /// <typeparam name="TChannel"></typeparam>
  19. public class OperationInvoker<TChannel> : OperationInvoker
  20. {
  21. /// <summary>
  22. /// 终点名
  23. /// </summary>
  24. public string EndpointName
  25. {
  26. get;
  27. private set;
  28. }
  29. /// <summary>
  30. /// 服务操作者
  31. /// </summary>
  32. /// <param name="endpointName"></param>
  33. public OperationInvoker(string endpointName)
  34. {
  35. this.EndpointName = endpointName;
  36. }
  37. /// <summary>
  38. /// 执行
  39. /// </summary>
  40. /// <param name="serviceInvocation"></param>
  41. /// <param name="timeout">超时(分钟)</param>
  42. public void Invoke(Action<TChannel> serviceInvocation, double timeout = 0)
  43. {
  44. ChannelFactory<TChannel> channelFactory = ChannelFactories.GetFactory<TChannel>(this.EndpointName);
  45. TChannel channel = channelFactory.CreateChannel();
  46. IContextChannel icc = channel as IContextChannel;
  47. if (icc != null)
  48. {
  49. if (timeout > 0)
  50. {
  51. icc.OperationTimeout = TimeSpan.FromMinutes(timeout);
  52. }
  53. else
  54. {
  55. // 不能大于24天,否则异常。
  56. icc.OperationTimeout = TimeSpan.FromDays(20);
  57. }
  58. }
  59. Invoke(serviceInvocation, channel);
  60. }
  61. /// <summary>
  62. /// 执行
  63. /// </summary>
  64. /// <typeparam name="TResult"></typeparam>
  65. /// <param name="serviceInvocation"></param>
  66. /// <param name="timeout">超时(分钟)</param>
  67. /// <returns></returns>
  68. public TResult Invoke<TResult>(Func<TChannel, TResult> serviceInvocation, double timeout = 0)
  69. {
  70. ChannelFactory<TChannel> channelFactory = ChannelFactories.GetFactory<TChannel>(this.EndpointName);
  71. TChannel channel = channelFactory.CreateChannel();
  72. IContextChannel icc = channel as IContextChannel;
  73. if (icc != null)
  74. {
  75. if (timeout > 0)
  76. {
  77. icc.OperationTimeout = TimeSpan.FromMinutes(timeout);
  78. }
  79. else
  80. {
  81. // 不能大于24天,否则异常。
  82. icc.OperationTimeout = TimeSpan.FromDays(20);
  83. }
  84. }
  85. return Invoke(serviceInvocation, channel);
  86. }
  87. }
  88. }