/// <summary> /// 根据指定的系统编号,直接寻找WCF客户端地址 /// </summary> /// <typeparam name="T">客户端代理实例</typeparam> /// <param name="code">系统编码</param> /// <param name="bindingCfgName">自己指定绑定的名称,如果绑定为空,则根据绑定类型和配置项来决定绑定配置</param> /// <returns>WCF接口调用类型</returns> public static WcfClientProxyScope <T> CreateWcfClientProxy <T>(string bindingCfgName) { try { Type type = typeof(T); RegisterTypeIfMissing(type); WcfClientInvokeCfgInfo wcfClientCfgInfo = _WcfInvokeContainer[type]; if (string.IsNullOrEmpty(wcfClientCfgInfo.CredentialFileNameOrServerName)) { if (MB.Util.MyNetworkCredential.CurrentSelectedServerInfo == null) { throw new MB.Util.APPException("没有设置服务的配置信息,请检查节点AppSetting/WcfServers!"); } wcfClientCfgInfo.CredentialFileNameOrServerName = MB.Util.MyNetworkCredential.CurrentSelectedServerInfo.Credential; } MB.Util.Model.WcfCredentialInfo wcfCredentialInfo = createWcfCredentialByCfgAttribute(wcfClientCfgInfo); string gzip = System.Configuration.ConfigurationManager.AppSettings[ENABLE_GZIP_MESSAGE]; bool isGZipCustomBinding = !string.IsNullOrEmpty(gzip) && string.Compare(gzip, "True", true) == 0; System.ServiceModel.Channels.Binding binding = createBinding(wcfCredentialInfo, isGZipCustomBinding, string.Empty); string uri = buildEndpointAddress(wcfCredentialInfo, wcfClientCfgInfo); System.ServiceModel.EndpointAddress address = new EndpointAddress(uri); NetworkCredential credentialInfo = null; if (!string.IsNullOrEmpty(wcfCredentialInfo.UserName) && !string.IsNullOrEmpty(wcfCredentialInfo.Password)) { credentialInfo = new NetworkCredential(wcfCredentialInfo.UserName, wcfCredentialInfo.Password, wcfCredentialInfo.Domain); } T proxy = (T)createProxyInstance <T>(binding, address, credentialInfo); //如果每个调用都需要记录的话为产生很多日志,这里特处理一下。 if (!_MyInvokeCache.ContainsKey(uri)) { _MyInvokeCache.Add(uri, uri); MB.Util.TraceEx.Write(string.Format("开始根据地址{0} 创建WCF 客户端Channel Proxy{1}", uri, typeof(T).FullName)); } WcfClientProxyScope <T> proxyScope = new WcfClientProxyScope <T>(proxy); return(proxyScope); } catch (MB.Util.APPException aex) { throw aex; } catch (Exception ex) { throw new MB.Util.APPException("创建WCF 客户端代理出错!", APPMessageType.SysErrInfo, ex); } }
/// <summary> /// 调用WCF接口方法 /// </summary> /// <typeparam name="T">接口类型</typeparam> /// <typeparam name="TResult">返回值类型</typeparam> /// <param name="execute">执行委托</param> /// <returns>返回值</returns> public static TResult InvokeWcfMethod <T, TResult>(Func <T, TResult> execute) { using (WcfClientProxyScope <T> proxy = CreateWcfClientProxy <T>()) { return(execute(proxy.ClientProxy)); } }
/// <summary> /// 调用WCF接口方法 /// </summary> /// <typeparam name="T">接口类型</typeparam> /// <param name="execute">执行委托</param> public static void InvokeWcfMethod <T>(Action <T> execute) { using (WcfClientProxyScope <T> proxy = CreateWcfClientProxy <T>()) { execute(proxy.ClientProxy); } }