#region using System; using System.ServiceModel; #endregion namespace CSFrameworkV5.WebRef { /// /// WCF客户端代理层-基类 /// /// public class WCF_Base where T : class { private void CloseService(ICommunicationObject svc) { try { //查看连接状态,关闭WCF var state = svc.State; //创建WCF通道,默认State=Opened; svc.Close(); //svc.Close();//测试关闭状态下,调用Close } catch { } } /// /// 执行WCF方法 /// /// 调用WCF接口核心逻辑 protected void Excute(Action action) { T svc = null; try { svc = WCFFactory.Create(); action(svc); CloseService(svc as ICommunicationObject); //关闭WCF服务 } catch (Exception ex) { if (svc != null) CloseService(svc as ICommunicationObject); throw ex; } } /// /// 执行WCF方法 /// /// 数据类型 /// 调用WCF接口核心逻辑 /// protected R Excute(Func action) { T svc = null; try { svc = WCFFactory.Create(); //创建WCF接口实例 var result = action(svc); //调用接口逻辑 CloseService(svc as ICommunicationObject); //关闭WCF服务 return result; } catch (Exception ex) { if (svc != null) CloseService(svc as ICommunicationObject); throw ex; } } /// /// 执行指定WCF接口的方法 /// /// WCF接口 /// 数据类型 /// 调用WCF接口核心逻辑 /// protected R Excute(Func action) where M : class { M svc = null; try { svc = WCFFactory.Create(); //创建WCF接口实例 var result = action(svc); //调用接口逻辑 CloseService(svc as ICommunicationObject); //关闭WCF服务 return result; } catch (Exception ex) { if (svc != null) CloseService(svc as ICommunicationObject); throw ex; } } } }