using System; using System.Collections.Generic; using System.Text; using System.ServiceModel.Channels; using System.ServiceModel; namespace SiaSun.LMS.Common { public class WCFHelper { #region Wcf服务工厂 public static T Create(string url) { return Create(url, "WSHttpBinding"); } public static T Create(string url, string bing) { if (!string.IsNullOrEmpty(url)) { try { EndpointAddress address = new EndpointAddress(url); System.ServiceModel.Channels.Binding binding = CreateBinding(bing); ChannelFactory factory = new ChannelFactory(binding, address); foreach (System.ServiceModel.Description.OperationDescription op in factory.Endpoint.Contract.Operations) { System.ServiceModel.Description.DataContractSerializerOperationBehavior dataContractBehavior = op.Behaviors.Find() as System.ServiceModel.Description.DataContractSerializerOperationBehavior; if (dataContractBehavior != null) { dataContractBehavior.MaxItemsInObjectGraph = 2147483647; } } return factory.CreateChannel(); } catch (Exception ex) { throw ex; } } return default(T); } #endregion #region 创建传输协议 /// /// 创建传输协议 /// /// 传输协议名称 /// public static System.ServiceModel.Channels.Binding CreateBinding(string binding) { System.ServiceModel.Channels.Binding bindinginstance = null; if (binding.ToLower() == "basichttpbinding") { BasicHttpBinding ws = new BasicHttpBinding(); bindinginstance = ws; } else if (binding.ToLower() == "wshttpbinding") { BasicHttpBinding ws = new BasicHttpBinding(); ws.CloseTimeout = new TimeSpan(0, 5, 0); ws.OpenTimeout = new TimeSpan(0, 5, 0); ws.ReceiveTimeout = new TimeSpan(0, 5, 0); ws.SendTimeout = new TimeSpan(0, 5, 0); ws.ReaderQuotas.MaxArrayLength = 2147483647; ws.ReaderQuotas.MaxStringContentLength = 2147483647; ws.ReaderQuotas.MaxBytesPerRead = 2147483647; ws.MaxBufferSize = 2147483647; ws.MaxBufferPoolSize = 2147483647; ws.MaxReceivedMessageSize = 2147483647; ws.TextEncoding = Encoding.UTF8; ws.MessageEncoding = WSMessageEncoding.Text; bindinginstance = ws; } else if (binding.ToLower() == "netnamedpipebinding") { NetNamedPipeBinding ws = new NetNamedPipeBinding(); bindinginstance = ws; } else if (binding.ToLower() == "netpeertcpbinding") { NetPeerTcpBinding ws = new NetPeerTcpBinding(); bindinginstance = ws; } else if (binding.ToLower() == "nettcpbinding") { NetTcpBinding ws = new NetTcpBinding(); bindinginstance = ws; } else if (binding.ToLower() == "wsdualhttpbinding") { WSDualHttpBinding ws = new WSDualHttpBinding(); bindinginstance = ws; } else if (binding.ToLower() == "wsfederationhttpbinding") { WSFederationHttpBinding ws = new WSFederationHttpBinding(); bindinginstance = ws; } return bindinginstance; } #endregion } }