宜昌华友原料库管理软件
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
4.1 KiB

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<T>(string url)
{
return Create<T>(url, "WSHttpBinding");
}
public static T Create<T>(string url, string bing)
{
if (!string.IsNullOrEmpty(url))
{
try
{
EndpointAddress address = new EndpointAddress(url);
System.ServiceModel.Channels.Binding binding = CreateBinding(bing);
ChannelFactory<T> factory = new ChannelFactory<T>(binding, address);
foreach (System.ServiceModel.Description.OperationDescription op in factory.Endpoint.Contract.Operations)
{
System.ServiceModel.Description.DataContractSerializerOperationBehavior dataContractBehavior =
op.Behaviors.Find<System.ServiceModel.Description.DataContractSerializerOperationBehavior>()
as System.ServiceModel.Description.DataContractSerializerOperationBehavior;
if (dataContractBehavior != null)
{
dataContractBehavior.MaxItemsInObjectGraph = 2147483647;
}
}
return factory.CreateChannel();
}
catch (Exception ex)
{
throw ex;
}
}
return default(T);
}
#endregion
#region ´´½¨´«ÊäЭÒé
/// <summary>
/// ´´½¨´«ÊäЭÒé
/// </summary>
/// <param name="binding">´«ÊäЭÒéÃû³Æ</param>
/// <returns></returns>
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
}
}