ServiceClientWrapper.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ServiceModel;
  6. using System.ServiceModel.Channels;
  7. using Aitex.Core.RT.Log;
  8. using System.Windows;
  9. using Aitex.Core.Utilities;
  10. using System.Threading;
  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 bool ActionFailed
  21. {
  22. get
  23. {
  24. return _isInError;
  25. }
  26. }
  27. public ServiceClientWrapper(string endpointConfigurationName, string name)
  28. {
  29. _serviceName = name;
  30. try
  31. {
  32. _factory = new ChannelFactory<T>(endpointConfigurationName);
  33. }
  34. catch (Exception ex)
  35. {
  36. MessageBox.Show("不能创建Service " + name + ",检查配置:" + endpointConfigurationName);
  37. LOG.Error("不能创建Service "+name+",检查配置:"+endpointConfigurationName, ex);
  38. }
  39. }
  40. public ServiceClientWrapper(string endpointConfigurationName, string name, EndpointAddress address)
  41. {
  42. _serviceName = name;
  43. try
  44. {
  45. _factory = new ChannelFactory<T>(endpointConfigurationName, address);
  46. }
  47. catch (Exception ex)
  48. {
  49. MessageBox.Show("不能创建Service " + name + ",检查配置:" + endpointConfigurationName);
  50. LOG.Error("不能创建Service " + name + ",检查配置:" + endpointConfigurationName, ex);
  51. }
  52. }
  53. public void Invoke(Action<T> action)
  54. {
  55. if (_factory == null)
  56. return;
  57. if (_proxy == null)
  58. {
  59. _proxy = _factory.CreateChannel();
  60. }
  61. for (int i = 0; i < 2; i++)
  62. {
  63. if (Do(action))
  64. break;
  65. Thread.Sleep(10);
  66. }
  67. }
  68. bool Do(Action<T> action)
  69. {
  70. if (_proxy != null && ((IClientChannel)_proxy).State == CommunicationState.Faulted)
  71. {
  72. ((IClientChannel)_proxy).Abort();
  73. _proxy = _factory.CreateChannel();
  74. }
  75. try
  76. {
  77. action(_proxy);
  78. if (_isInError)
  79. {
  80. _isInError = false;
  81. LOG.Info(_serviceName + " 服务恢复");
  82. }
  83. return true;
  84. }
  85. catch (EndpointNotFoundException ex)
  86. {
  87. if (!_isInError)
  88. {
  89. _isInError = true;
  90. LOG.Error(_serviceName + " 连接已经断开.", ex);
  91. }
  92. }
  93. catch (ProtocolException ex)
  94. {
  95. if (!_isInError)
  96. {
  97. _isInError = true;
  98. LOG.Error(_serviceName + " 服务程序异常.", ex);
  99. }
  100. }
  101. catch (Exception ex)
  102. {
  103. if (!_isInError)
  104. {
  105. _isInError = true;
  106. LOG.Error(_serviceName + " 服务异常", ex);
  107. }
  108. }
  109. ((IClientChannel)_proxy).Abort();
  110. _proxy = _factory.CreateChannel();
  111. return false;
  112. }
  113. private Binding CreateBinding(string binding)
  114. {
  115. Binding bindinginstance = null;
  116. if (binding.ToLower() == "basichttpbinding")
  117. {
  118. BasicHttpBinding ws = new BasicHttpBinding();
  119. ws.MaxBufferSize = 2147483647;
  120. ws.MaxBufferPoolSize = 2147483647;
  121. ws.MaxReceivedMessageSize = 2147483647;
  122. ws.ReaderQuotas.MaxStringContentLength = 2147483647;
  123. ws.CloseTimeout = new TimeSpan(0, 10, 0);
  124. ws.OpenTimeout = new TimeSpan(0, 10, 0);
  125. ws.ReceiveTimeout = new TimeSpan(0, 10, 0);
  126. ws.SendTimeout = new TimeSpan(0, 10, 0);
  127. bindinginstance = ws;
  128. }
  129. else if (binding.ToLower() == "netnamedpipebinding")
  130. {
  131. NetNamedPipeBinding ws = new NetNamedPipeBinding();
  132. ws.MaxReceivedMessageSize = 65535000;
  133. bindinginstance = ws;
  134. }
  135. else if (binding.ToLower() == "nettcpbinding")
  136. {
  137. NetTcpBinding ws = new NetTcpBinding();
  138. ws.MaxReceivedMessageSize = 65535000;
  139. ws.Security.Mode = SecurityMode.None;
  140. bindinginstance = ws;
  141. }
  142. else if (binding.ToLower() == "wsdualhttpbinding")
  143. {
  144. WSDualHttpBinding ws = new WSDualHttpBinding();
  145. ws.MaxReceivedMessageSize = 65535000;
  146. bindinginstance = ws;
  147. }
  148. else if (binding.ToLower() == "webhttpbinding")
  149. {
  150. //WebHttpBinding ws = new WebHttpBinding();
  151. //ws.MaxReceivedMessageSize = 65535000;
  152. //bindinginstance = ws;
  153. }
  154. else if (binding.ToLower() == "wsfederationhttpbinding")
  155. {
  156. WSFederationHttpBinding ws = new WSFederationHttpBinding();
  157. ws.MaxReceivedMessageSize = 65535000;
  158. bindinginstance = ws;
  159. }
  160. else if (binding.ToLower() == "wshttpbinding")
  161. {
  162. WSHttpBinding ws = new WSHttpBinding(SecurityMode.None);
  163. ws.MaxReceivedMessageSize = 65535000;
  164. ws.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.Windows;
  165. ws.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Windows;
  166. bindinginstance = ws;
  167. }
  168. return bindinginstance;
  169. }
  170. }
  171. }