/// <summary> /// 调用WCF /// </summary> /// <param name="ContractType"></param> /// <param name="BindingName"></param> /// <param name="Endaddress"></param> /// <param name="FuncName"></param> /// <param name="strValue"></param> /// <returns></returns> public static object CallEventWCFService(string ContractType, string BindingName, string Endaddress, string FuncName, string strValue) { EndpointAddress endPoint = new EndpointAddress(Endaddress);//地址 string XmlTemplete = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" + "\r\n" + "<System>" + "\r\n" + "{0}" + "</System>"; if (ContractType.ToUpper() == "ENGINE") { IApplicationService contract = new ChannelFactory<IApplicationService>(WCfBindingName(BindingName), endPoint).CreateChannel(); Type type = contract.GetType(); MethodInfo methodInfo = type.GetMethod(FuncName); strValue = string.Format(XmlTemplete, strValue); string[] param = new string[1]; param[0] = strValue; return methodInfo.Invoke(contract, param); } else { using (ChannelFactory<IEventTriggerProcess> channel = new ChannelFactory<IEventTriggerProcess>(WCfBindingName(BindingName), endPoint)) { IEventTriggerProcess instance = channel.CreateChannel(); using (instance as IDisposable) { try { Type type = typeof(IEventTriggerProcess); MethodInfo mi = type.GetMethod(FuncName); strValue = string.Format(XmlTemplete, strValue); string[] param = new string[1]; param[0] = strValue; //Log.WriteLog("调用服务验证Endaddress:" + Endaddress + "strValue:" + strValue); return mi.Invoke(instance, param); } catch (TimeoutException tm) { (instance as ICommunicationObject).Abort(); //Log.WriteLog("调用服务验证TimeoutException" + tm.StackTrace); throw; } catch (CommunicationException com) { (instance as ICommunicationObject).Abort(); //Log.WriteLog("调用服务验证CommunicationException" + com.StackTrace); throw; } catch (Exception vErr) { //Log.WriteLog("调用服务验证Exception" + vErr.StackTrace); (instance as ICommunicationObject).Abort(); throw; } } } } }
/// <summary> /// 流程调用的方法 /// </summary> /// <param name="BindingName">bindingName</param> /// <param name="Endaddress">endaddress</param> /// <param name="FuncName">funcName</param> /// <param name="strValue">strValue</param> /// <returns>object</returns> public static object CallWCFService(string bindingName, string endaddress, string funcName, string strValue,ref string erroMesssage) { //绑定不同的协议未测试,讨论一下最好的方法,或者或者做一个CASE,让用户填好 try { EndpointAddress endPoint = new EndpointAddress(endaddress);//地址 IApplicationService contract = new ChannelFactory<IApplicationService>(WCfBindingName(bindingName), endPoint).CreateChannel(); Type type = contract.GetType(); MethodInfo methodInfo = type.GetMethod(funcName); string[] param = new string[1]; param[0] = strValue; return methodInfo.Invoke(contract, param); } catch (Exception e) { string cMessage = "\r\n 动态 调用第三方服务出错 命名空间:SMT.Workflow.Engine.BLL 类方法:CallWCFService() WCF地址:" + endaddress + "方法名称:" +funcName + " " + "WCF协议:" + bindingName + "\r\n" + "参数:" + strValue + "\r\n"; Tracer.Debug(cMessage+e.ToString()); erroMesssage += cMessage; return null; //throw new Exception("命名空间:SMT.Workflow.Engine.BLL 类方法:CallWCFService()" + e.Message); //暂时不抛出异常也能往下走 } }