AlertServiceClientFactory.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System;
  2. using System.ServiceModel;
  3. using System.ServiceModel.Channels;
  4. using System.Configuration;
  5. namespace IntelligentControlForsx
  6. {
  7. /// <summary>
  8. /// 动态调用WCF的工具类库
  9. /// </summary>
  10. public class AlertServiceClientFactory
  11. {
  12. public static AlertServiceClient CreateAlertServiceClient()
  13. {
  14. string url = ConfigurationManager.AppSettings["WCFServiceUrl"]; // "http://localhost:8733/GDNXFD/AlertService/";
  15. string bing = ConfigurationManager.AppSettings["WCFServiceBinding"]; //"basichttpbinding";
  16. EndpointAddress address = new EndpointAddress(url);
  17. Binding binding = CreateBinding(bing);
  18. return new AlertServiceClient(binding, address);
  19. }
  20. #region Wcf服务工厂
  21. public static T CreateWCFServiceByURL<T>(string url)
  22. {
  23. return CreateWCFServiceByURL<T>(url, "wsHttpBinding");
  24. }
  25. public static T CreateWCFServiceByURL<T>(string url, string bing)
  26. {
  27. if (string.IsNullOrEmpty(url)) throw new NotSupportedException("this url isn`t Null or Empty!");
  28. EndpointAddress address = new EndpointAddress(url);
  29. Binding binding = CreateBinding(bing);
  30. ChannelFactory<T> factory = new ChannelFactory<T>(binding, address);
  31. return factory.CreateChannel();
  32. }
  33. #endregion
  34. #region 创建传输协议
  35. /// <summary>
  36. /// 创建传输协议
  37. /// </summary>
  38. /// <param name="binding">传输协议名称</param>
  39. /// <returns></returns>
  40. private static Binding CreateBinding(string binding)
  41. {
  42. Binding bindinginstance = null;
  43. if (binding.ToLower() == "basichttpbinding")
  44. {
  45. BasicHttpBinding ws = new BasicHttpBinding();
  46. ws.MaxBufferSize = 2147483647;
  47. ws.MaxBufferPoolSize = 2147483647;
  48. ws.MaxReceivedMessageSize = 2147483647;
  49. ws.ReaderQuotas.MaxStringContentLength = 2147483647;
  50. ws.CloseTimeout = new TimeSpan(0, 10, 0);
  51. ws.OpenTimeout = new TimeSpan(0, 10, 0);
  52. ws.ReceiveTimeout = new TimeSpan(0, 10, 0);
  53. ws.SendTimeout = new TimeSpan(0, 10, 0);
  54. bindinginstance = ws;
  55. }
  56. else if (binding.ToLower() == "netnamedpipebinding")
  57. {
  58. NetNamedPipeBinding ws = new NetNamedPipeBinding();
  59. ws.MaxReceivedMessageSize = 65535000;
  60. bindinginstance = ws;
  61. }
  62. else if (binding.ToLower() == "netpeertcpbinding")
  63. {
  64. NetPeerTcpBinding ws = new NetPeerTcpBinding();
  65. ws.MaxReceivedMessageSize = 65535000;
  66. bindinginstance = ws;
  67. }
  68. else if (binding.ToLower() == "nettcpbinding")
  69. {
  70. NetTcpBinding ws = new NetTcpBinding();
  71. ws.MaxReceivedMessageSize = 65535000;
  72. ws.Security.Mode = SecurityMode.None;
  73. bindinginstance = ws;
  74. }
  75. else if (binding.ToLower() == "wsdualhttpbinding")
  76. {
  77. WSDualHttpBinding ws = new WSDualHttpBinding();
  78. ws.MaxReceivedMessageSize = 65535000;
  79. bindinginstance = ws;
  80. }
  81. else if (binding.ToLower() == "webhttpbinding")
  82. {
  83. //WebHttpBinding ws = new WebHttpBinding();
  84. //ws.MaxReceivedMessageSize = 65535000;
  85. //bindinginstance = ws;
  86. }
  87. else if (binding.ToLower() == "wsfederationhttpbinding")
  88. {
  89. WSFederationHttpBinding ws = new WSFederationHttpBinding();
  90. ws.MaxReceivedMessageSize = 65535000;
  91. bindinginstance = ws;
  92. }
  93. else if (binding.ToLower() == "wshttpbinding")
  94. {
  95. WSHttpBinding ws = new WSHttpBinding(SecurityMode.None);
  96. ws.MaxReceivedMessageSize = 65535000;
  97. ws.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.Windows;
  98. ws.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Windows;
  99. bindinginstance = ws;
  100. }
  101. return bindinginstance;
  102. }
  103. #endregion
  104. }
  105. }