| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
-
- using System;
- using System.ServiceModel;
- //using Curtain.WCF.Contract;
- namespace Curtain.WCF.Proxy
- {
- /// <summary>
- /// WCF服务代理基类
- /// </summary>
- public abstract class WCFProxy<TChannel> : IDisposable//, IWCFProxy
- where TChannel : class//, IWCFContract
- {
- #region 常量
- #endregion
- #region 成员变量
- /// <summary>
- /// 终结点的配置名称
- /// </summary>
- private string _endpointName = null;
- private string _serviceName = null;
- /// <summary>
- /// WCF通道工厂
- /// </summary>
- private ChannelFactory<TChannel> _channelFactory = null;
- /// <summary>
- /// WCF代理配置
- /// </summary>
- private WCFProxySetting _setting = null;
- /// <summary>
- /// WCF代理配置版本
- /// </summary>
- private int _settingVersion = 0;
- #endregion
- #region 构造
- /// <summary>
- /// WCF服务代理基类
- /// </summary>
- /// <param name="endpointName"></param>
- /// <param name="settingName"></param>
- /// <param name="serviceName"></param>
- 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);
- }
- /// <summary>
- /// WCF服务代理基类
- /// </summary>
- /// <param name="endpointName"></param>
- /// <param name="setting"></param>
- 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;
- }
- }
- /// <summary>
- /// WCF代理配置
- /// </summary>
- public WCFProxySetting ProxySetting
- {
- get
- {
- return this._setting;
- }
- }
- /// <summary>
- /// 信道工厂
- /// </summary>
- public ChannelFactory<TChannel> ChannelFactory
- {
- get
- {
- return this._channelFactory;
- }
- }
- #endregion
- #region 公有方法
- /// <summary>
- /// 执行
- /// </summary>
- /// <param name="serviceInvocation"></param>
- /// <param name="timeout">超时(分钟)</param>
- public virtual void Invoke(Action<TChannel> 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<object>(methodName, null, true);
- }
- catch (Exception ex)
- {
- Exception newEx = this.InvokeException(methodName, ex);
- if (newEx == null)
- {
- return;
- }
- throw newEx;
- }
- finally
- {
- if (contextChannel != null)
- {
- try
- {
- contextChannel.Close();
- }
- catch
- {
- contextChannel.Abort();
- }
- }
- }
- }
- /// <summary>
- /// 执行
- /// </summary>
- /// <typeparam name="TResult"></typeparam>
- /// <param name="serviceInvocation"></param>
- /// <param name="timeout">超时(分钟)</param>
- /// <returns></returns>
- public virtual TResult Invoke<TResult>(Func<TChannel, TResult> 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
- //if(channel == null)
- {
- 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);
- }
- }
- }
- TResult result = serviceInvocation(channel);
- result = this.InvokeDone<TResult>(methodName, result);
- return result;
- }
- catch (Exception ex)
- {
- Exception newEx = this.InvokeException(methodName, ex);
- if (newEx == null)
- {
- return default(TResult);
- }
- throw newEx;
- }
- finally
- {
- if (contextChannel != null)
- {
- try
- {
- contextChannel.Close();
- }
- catch
- {
- contextChannel.Abort();
- }
- }
- }
- }
- /// <summary>
- /// 关闭通道工厂
- /// </summary>
- public void CloseChannelFactory()
- {
- try
- {
- this._channelFactory.Close();
- }
- catch
- {
- this._channelFactory.Abort();
- }
- finally
- {
- this._channelFactory = null;
- }
- }
- /// <summary>
- /// 释放分配的资源
- /// </summary>
- public void Dispose()
- {
- this._setting = null;
- this.CloseChannelFactory();
- }
- #endregion
- #region 保护方法
- protected virtual TChannel GetServerInstance()
- {
- //throw new NotImplementedException();
- return null;
- }
- /// <summary>
- /// 异常处理
- /// 1.写日志
- /// 2.客户端连接异常处理
- /// 3.服务端异常处理
- /// </summary>
- /// <param name="cre"></param>
- /// <param name="sre"></param>
- /// <param name="ex"></param>
- /// <returns>抛出未处理异常</returns>
- protected virtual Exception InvokeException(string methodName, Exception ex)
- {
- return ex;
- }
- /// <summary>
- /// 请求被处理后
- /// 1.如果服务端异常已处理,写日志,提示消息等处理。
- /// </summary>
- /// <param name="result"></param>
- protected virtual TResult InvokeDone<TResult>(string methodName, TResult result, bool isOneWay = false)
- {
- return result;
- }
- #endregion
- #region 私有方法
- /// <summary>
- /// 打开通道工厂
- /// </summary>
- protected void OpenChannelFactory()
- {
- lock (this)
- {
- if (this._channelFactory != null &&
- this._settingVersion != this._setting.Version)
- {
- this.CloseChannelFactory();
- }
- if (this._channelFactory == null)
- {
- this._settingVersion = this._setting.Version;
- EndpointAddress remoteAddress = new EndpointAddress(this._setting.GetEndpointAddress(this._serviceName));
- this._channelFactory = new ChannelFactory<TChannel>(this._endpointName, remoteAddress);
- this._channelFactory.Open();
- }
- }
- }
- #endregion
- }
- }
|