#region
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
using CSFrameworkV5.WCFContract;
#endregion
namespace CSFrameworkV5.WebRef
{
///
/// 动态调用WCF接口,动态创建WCF通道
///
public class WCFInvokeContext
{
#region 创建传输协议
///
/// 创建传输协议
///
/// 传输协议名称
///
private static Binding CreateBinding(BindingType binding)
{
Binding bindinginstance = null;
//BasicHttp传输协议类型,其它类型的协议参考下面的参数配置
if (binding == BindingType.BasicHttpBinding)
{
var ws = new BasicHttpBinding();
ws.MaxReceivedMessageSize = 2147483647; //最大接收的消息大小
ws.MaxBufferSize = 2147483647; // 从通道接收消息的缓存大小
ws.MaxBufferPoolSize = 2147483647; //从通道接收消息的最大缓存数量
ws.CloseTimeout = new TimeSpan(5, 0, 0); //设5小时
ws.OpenTimeout = new TimeSpan(5, 0, 0);
ws.SendTimeout = new TimeSpan(5, 0, 0);
ws.ReceiveTimeout = new TimeSpan(5, 0, 0);
var rq = ws.ReaderQuotas;
rq.MaxArrayLength = 2147483647; //最大数组长度
rq.MaxBytesPerRead = 6553600; //最大每次读取长度
rq.MaxDepth = 6553600; // 最大节点深度
rq.MaxNameTableCharCount = 6553600; //最大NameTableChar的数量
rq.MaxStringContentLength = 2147483647; // 最大内容长度
bindinginstance = ws;
}
else if (binding == BindingType.NetTcpBinding)
{
var ws = new NetTcpBinding();
ws.Security.Mode = SecurityMode.None;
bindinginstance = ws;
ws.MaxReceivedMessageSize = 2147483647; //最大接收的消息大小
ws.MaxBufferSize = 2147483647; // 从通道接收消息的缓存大小
ws.MaxBufferPoolSize = 2147483647; //从通道接收消息的最大缓存数量
ws.CloseTimeout = new TimeSpan(0, 10, 0);
ws.OpenTimeout = new TimeSpan(0, 10, 0);
ws.SendTimeout = new TimeSpan(0, 10, 0);
ws.ReceiveTimeout = new TimeSpan(0, 10, 0);
var rq = ws.ReaderQuotas;
rq.MaxArrayLength = 2147483647; //最大数组长度
rq.MaxBytesPerRead = 6553600; //最大每次读取长度
rq.MaxDepth = 6553600; // 最大节点深度
rq.MaxNameTableCharCount = 6553600; //最大NameTableChar的数量
rq.MaxStringContentLength = 2147483647; // 最大内容长度
bindinginstance = ws;
}
else if (binding == BindingType.wsHttpBinding)
{
var ws = new WSHttpBinding(SecurityMode.None);
ws.MaxReceivedMessageSize = 65535000;
ws.Security.Message.ClientCredentialType =
MessageCredentialType.Windows;
ws.Security.Transport.ClientCredentialType =
HttpClientCredentialType.Windows;
bindinginstance = ws;
}
return bindinginstance;
}
#endregion
///
/// 动态调用WCF的方法
///
/// WCF接口
/// 地址
/// 方法名
/// 参数
///
public static object ExecuteMethod(string url, string methodName,
params object[] Params)
{
var address = new EndpointAddress(url);
Binding bindinginstance = null;
//HTTP协议
var ht = new BasicHttpBinding();
ht.MaxReceivedMessageSize = 20971520;
ht.Security.Mode = BasicHttpSecurityMode.None;
bindinginstance = ht;
using (var channel =
new ChannelFactory(bindinginstance, address))
{
var instance = channel.CreateChannel();
using (instance as IDisposable)
{
try
{
var type = typeof(T);
var mi = type.GetMethod(methodName);
return mi.Invoke(instance, Params);
}
catch (TimeoutException)
{
(instance as ICommunicationObject).Abort();
throw;
}
catch (CommunicationException)
{
(instance as ICommunicationObject).Abort();
throw;
}
catch (Exception)
{
(instance as ICommunicationObject).Abort();
throw;
}
}
}
}
#region WCF服务工厂 Create方法
///
/// 创建WCF服务客户端实例
///
/// 接口类型,WCF契约
/// WCF服务地址
/// 返回WCF服务客户端实例
public static T CreateWCFService(string url)
{
return CreateWCFService(url, BindingType.BasicHttpBinding);
}
///
/// 创建WCF服务客户端实例
///
/// 接口类型,WCF契约
/// WCF服务地址
/// 协议类型
/// 返回WCF服务客户端实例
public static T CreateWCFService(string url, BindingType bing)
{
var address = new EndpointAddress(url);
var binding = CreateBinding(bing);
var factory = new ChannelFactory(binding, address);
return factory.CreateChannel();
}
///
/// 创建WCF服务的客户端实例
///
/// 接口类型,WCF契约
/// WCF服务地址
/// 协议
/// 返回WCF的客户端实例
public static T CreateWCFService(string url, Binding binding)
{
var address = new EndpointAddress(url);
var factory = new ChannelFactory(binding, address);
return factory.CreateChannel();
}
#endregion
}
}