using System;
using System.ServiceModel;
//using Curtain.WCF.Contract;
namespace Curtain.WCF.Proxy
{
///
/// WCF服务代理基类
///
public abstract class WCFProxy : IDisposable//, IWCFProxy
where TChannel : class//, IWCFContract
{
#region 常量
#endregion
#region 成员变量
///
/// 终结点的配置名称
///
private string _endpointName = null;
private string _serviceName = null;
///
/// WCF通道工厂
///
private ChannelFactory _channelFactory = null;
///
/// WCF代理配置
///
private WCFProxySetting _setting = null;
///
/// WCF代理配置版本
///
private int _settingVersion = 0;
#endregion
#region 构造
///
/// WCF服务代理基类
///
///
///
///
protected WCFProxy(string endpointName, string settingName, string serviceName)
{
if (string.IsNullOrWhiteSpace(endpointName))
{
throw new ArgumentNullException("endpointName");
}
this._endpointName = endpointName;
this._serviceName = serviceName;
this._setting = WCFProxySetting.CreateSetting(settingName);
}
///
/// WCF服务代理基类
///
///
///
protected WCFProxy(string endpointName, WCFProxySetting setting, string serviceName)
{
if (string.IsNullOrWhiteSpace(endpointName))
{
throw new ArgumentNullException("endpointName");
}
this._endpointName = endpointName;
this._serviceName = serviceName;
this._setting = (setting == null ? WCFProxySetting.DefaultSetting : setting);
}
#endregion
#region 属性
public string EndpointName
{
get
{
return this._endpointName;
}
}
public string ServiceName
{
get
{
return this._serviceName;
}
}
///
/// WCF代理配置
///
public WCFProxySetting ProxySetting
{
get
{
return this._setting;
}
}
///
/// 信道工厂
///
public ChannelFactory ChannelFactory
{
get
{
return this._channelFactory;
}
}
#endregion
#region 公有方法
///
/// 执行
///
///
/// 超时(分钟)
public virtual void Invoke(Action serviceInvocation, double timeout = 0)
{
TChannel channel = null;//default(TChannel);
IContextChannel contextChannel = null;
string methodName = serviceInvocation.Method.ToString();
try
{
if (this._setting.Type == 1)
{
channel = this.GetServerInstance();
}
else
{
this.OpenChannelFactory();
channel = this._channelFactory.CreateChannel();
contextChannel = channel as IContextChannel;
if (contextChannel != null)
{
if (timeout > WCFProxySetting.OPERATION_TIMEOUT_MAX)
{
timeout = WCFProxySetting.OPERATION_TIMEOUT_MAX;
}
if (timeout > 0)
{
contextChannel.OperationTimeout = TimeSpan.FromMinutes(timeout);
}
}
}
serviceInvocation(channel);
this.InvokeDone