using System; using System.ServiceModel; using System.Windows.Forms; namespace RGD.MdsAPI.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)) MessageBox.Show("连接服务失败!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); try { EndpointAddress address = new EndpointAddress(url); System.ServiceModel.Channels.Binding binding = CreateBinding(bing); ChannelFactory factory = new ChannelFactory(binding, address); return factory.CreateChannel(); } catch (Exception ex) { MessageBox.Show("连接服务失败!\n" + ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information); return default(T); } } #endregion Wcf服务工厂 #region 创建传输协议 /// /// 创建传输协议 /// /// 传输协议名称 /// public static System.ServiceModel.Channels.Binding CreateBinding(string binding) { System.ServiceModel.Channels.Binding bindinginstance = null; if (binding.ToLower() == "basichttpbinding") { BasicHttpBinding ws = new BasicHttpBinding(); ws.MaxReceivedMessageSize = 65535000; //System.Xml.XmlDictionaryReaderQuotas quotas = new System.Xml.XmlDictionaryReaderQuotas(); //quotas.MaxStringContentLength = 6553500; //ws.ReaderQuotas = quotas; ws.SendTimeout = new TimeSpan(0, 5, 1); ws.ReceiveTimeout = new TimeSpan(0, 5, 1); bindinginstance = ws; } else if (binding.ToLower() == "netnamedpipebinding") { NetNamedPipeBinding ws = new NetNamedPipeBinding(); // ws.MaxReceivedMessageSize = 65535000; bindinginstance = ws; } //else if (binding.ToLower() == "netpeertcpbinding") //{ // NetPeerTcpBinding ws = new NetPeerTcpBinding(); // ws.MaxReceivedMessageSize = 65535000; // bindinginstance = ws; //} else if (binding.ToLower() == "nettcpbinding") { NetTcpBinding ws = new NetTcpBinding(); ws.MaxReceivedMessageSize = 65535000; ws.Security.Mode = SecurityMode.None; bindinginstance = ws; } else if (binding.ToLower() == "wsdualhttpbinding") { WSDualHttpBinding ws = new WSDualHttpBinding(); ws.MaxReceivedMessageSize = 65535000; bindinginstance = ws; } //else if (binding.ToLower() == "webhttpbinding") //{ // WebHttpBinding ws = new WebHttpBinding(); // ws.MaxReceivedMessageSize = 65535000; // bindinginstance = ws; //} else if (binding.ToLower() == "wsfederationhttpbinding") { WSFederationHttpBinding ws = new WSFederationHttpBinding(); ws.MaxReceivedMessageSize = 65535000; bindinginstance = ws; } else if (binding.ToLower() == "wshttpbinding") { BasicHttpBinding ws = new BasicHttpBinding(BasicHttpSecurityMode.None); ws.MaxReceivedMessageSize = 65535000; ws.SendTimeout = new TimeSpan(0, 5, 1); ws.ReceiveTimeout = new TimeSpan(0, 5, 1); bindinginstance = ws; // ws.Security.Message.ClientCredentialType = System.ServiceModel.MessageCredentialType.Windows; // ws.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.Windows; } return bindinginstance; } #endregion 创建传输协议 } }