ServiceClientWrapper.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ServiceModel;
  6. using Aitex.Core.RT.Log;
  7. using System.Windows;
  8. using Aitex.Core.Utilities;
  9. using System.Threading;
  10. using System.Reflection;
  11. namespace Aitex.Core.WCF
  12. {
  13. public class ServiceClientWrapper<T>
  14. {
  15. ChannelFactory<T> _factory;
  16. T _proxy;
  17. bool _isInError = false;
  18. string _serviceName;
  19. Retry _retryConnect = new Retry();
  20. public ServiceClientWrapper(string endpointConfigurationName, string name)
  21. {
  22. _serviceName = name;
  23. try
  24. {
  25. _factory = new ChannelFactory<T>(endpointConfigurationName);
  26. }
  27. catch (Exception ex)
  28. {
  29. MessageBox.Show("不能创建Service " + name + ",检查配置:" + endpointConfigurationName);
  30. LOG.WriteExeption("不能创建Service "+name+",检查配置:"+endpointConfigurationName, ex);
  31. }
  32. }
  33. public ServiceClientWrapper(string endpointConfigurationName, string name, EndpointAddress address)
  34. {
  35. _serviceName = name;
  36. try
  37. {
  38. _factory = new ChannelFactory<T>(endpointConfigurationName, address);
  39. }
  40. catch (Exception ex)
  41. {
  42. MessageBox.Show("不能创建Service " + name + ",检查配置:" + endpointConfigurationName);
  43. LOG.WriteExeption("不能创建Service " + name + ",检查配置:" + endpointConfigurationName, ex);
  44. }
  45. }
  46. public void Invoke(Action<T> action)
  47. {
  48. if (_factory == null)
  49. return;
  50. if (_proxy == null)
  51. {
  52. _proxy = _factory.CreateChannel();
  53. }
  54. for (int i = 0; i < 2; i++)
  55. {
  56. if (Do(action))
  57. break;
  58. Thread.Sleep(10);
  59. }
  60. }
  61. bool Do(Action<T> action)
  62. {
  63. if (_proxy != null && ((IClientChannel)_proxy).State == CommunicationState.Faulted)
  64. {
  65. ((IClientChannel)_proxy).Abort();
  66. _proxy = _factory.CreateChannel();
  67. }
  68. DateTime dateTime = DateTime.Now;
  69. string method = "";
  70. try
  71. {
  72. MethodInfo methodInfo= action.GetMethodInfo();
  73. method = methodInfo.Name;
  74. action(_proxy);
  75. if (_isInError)
  76. {
  77. _isInError = false;
  78. //LOG.Info(_serviceName + " 服务恢复");
  79. }
  80. return true;
  81. }
  82. catch (EndpointNotFoundException ex)
  83. {
  84. if (!_isInError)
  85. {
  86. _isInError = true;
  87. LOG.WriteExeption(_serviceName + " disconnect。", ex);
  88. }
  89. }
  90. catch (ProtocolException ex)
  91. {
  92. if (!_isInError)
  93. {
  94. _isInError = true;
  95. LOG.WriteExeption($"{_serviceName} service method {method} start {dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff")} process exception。", ex);
  96. }
  97. }
  98. catch (Exception ex)
  99. {
  100. if (!_isInError)
  101. {
  102. _isInError = true;
  103. LOG.WriteExeption($"{_serviceName} service method {method} start {dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff")} exception", ex);
  104. }
  105. }
  106. ((IClientChannel)_proxy).Abort();
  107. _proxy = _factory.CreateChannel();
  108. return false;
  109. }
  110. }
  111. }