/// <summary> /// 验证并预处理请求参数 /// </summary> /// <param name="request"></param> private static void InvalidRequest(ESB.Core.Schema.务请求 request) { if (String.IsNullOrEmpty(request.主机名称)) { throw LogUtil.ExceptionAndLog("主机名称必须填写!", "", request); } if (String.IsNullOrEmpty(request.务名称)) { throw LogUtil.ExceptionAndLog("服务名称必须填写!", "", request); } if (String.IsNullOrEmpty(request.方法名称)) { EntityList <BindingTemplate> bindings = GetBindings(request); if (bindings == null) { throw LogUtil.ExceptionAndLog("方法名称必须填写!", "", request); } else { throw LogUtil.ExceptionAndLog("方法名称必须填写!", "", bindings[0], request); } } //--如果用户没有传递请求时间,或则传入时间无效,则默认为当前时间 if (request.请求时间.Year != DateTime.Now.Year) { request.请求时间 = DateTime.Now; } if (String.IsNullOrEmpty(request.消息内容)) { request.消息内容 = String.Empty; } if (String.IsNullOrEmpty(request.密码)) { request.密码 = String.Empty; } }
/// <summary> /// 根据绑定中的协议类型调用不同的实现方法 /// </summary> /// <param name="state"></param> /// <returns></returns> private static ESB.Core.Schema.务响应 CallService(CallState callState) { switch (callState.Binding.BindingType) { case (int)BindingType.WebService: return(SoapClient.CallWebService(callState)); case (int)BindingType.REST: return(RestfulClient.CallRestfulService(callState)); case (int)BindingType.ASHX: return(HandlerClient.CallAshxService(callState)); case (int)BindingType.WCF_HTTP: return(WcfClient.CallWcfService(callState)); default: throw LogUtil.ExceptionAndLog(callState, "未知的绑定类型", callState.Binding.BindingType.ToString(), callState.Binding, callState.Request); } }
/// <summary> /// 根据服务请求获取到对应的URL绑定信息 /// </summary> /// <param name="serviceName"></param> /// <returns></returns> private static EntityList <BindingTemplate> GetBindings(ESB.Core.Schema.务请求 request) { String serviceName = request.务名称; BusinessService service = BusinessService.FindByServiceName(serviceName); if (service == null) { throw LogUtil.ExceptionAndLog(String.Format("服务【{0}】尚未注册!", serviceName), "", request); } //--查找某个服务下所有启用状态的绑定 EntityList <BindingTemplate> bings = BindingTemplate.FindAllByServiceID(service.ServiceID); if (bings == null || bings.Count == 0) { throw LogUtil.ExceptionAndLog(String.Format("服务【{0}】尚未绑定任何路由地址!", serviceName), "", request); } return(bings); }
public static ESB.Core.Schema.务响应 CallAshxService(CallState callState) { //--STEP.1.从CallState中获取到需要的信息 ESB.Core.Schema.务请求 request = callState.Request; String message = callState.Request.消息内容; BindingTemplate binding = callState.Binding; String uri = callState.Binding.Address; String contentType = String.Equals(callState.Request.消息编码, "XML", StringComparison.OrdinalIgnoreCase) ? EsbClient.CONTENT_TYPE_XML : EsbClient.CONTENT_TYPE_JSON; //--STEP.2.根据method拼接URL if (String.IsNullOrEmpty(callState.Request.方法名称)) { throw LogUtil.ExceptionAndLog(callState, "ASHX服务的方法名称必须填写", "", binding, request); } else { uri = uri + "?EsbAction=" + callState.Request.方法名称; } //--STEP.3.构造HTTP请求并调用ASHX服务 ESB.Core.Schema.务响应 response = new ESB.Core.Schema.务响应(); try { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri); webRequest.Accept = contentType; webRequest.Method = "POST"; webRequest.ContentType = contentType; //--STEP.3.1.如果是POST请求,则需要将消息内容发送出去 if (!String.IsNullOrEmpty(message)) { byte[] data = System.Text.Encoding.Default.GetBytes(message); using (Stream stream = webRequest.GetRequestStream()) { stream.Write(data, 0, data.Length); } } //--STEP.3.2.获取到响应消息 callState.CallBeginTime = DateTime.Now; HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); using (Stream newstream = webResponse.GetResponseStream()) { using (StreamReader srRead = new StreamReader(newstream, System.Text.Encoding.Default)) { String outString = srRead.ReadToEnd(); callState.CallEndTime = DateTime.Now; response.消息内容 = outString; srRead.Close(); } } //--STEP.3.3.记录日志并返回ESB响应 LogUtil.AddAuditLog( 1 , binding , callState.RequestBeginTime, callState.RequestEndTime, callState.CallBeginTime, callState.CallEndTime , response.消息内容, request); } catch (Exception ex) { callState.CallEndTime = DateTime.Now; String exMessage = String.Empty; if (ex.InnerException != null) { exMessage = ex.InnerException.Message; } else { exMessage = ex.Message; } throw LogUtil.ExceptionAndLog(callState, "调用目标服务抛出异常", exMessage, binding, request); } return(response); }
/// <summary> /// 调用主流程 /// </summary> /// <param name="callState"></param> /// <returns></returns> public static ESB.Core.Schema.务响应 CallRestfulService(CallState callState) { //--STEP.1.从CallState中获取到需要的信息 ESB.Core.Schema.务请求 request = callState.Request; String message = callState.Request.消息内容; BindingTemplate binding = callState.Binding; String uri = callState.Binding.Address.TrimEnd('/'); String contentType = Constant.CONTENT_TYPE_JSON; String method = "POST"; String methodName = String.Empty; String[] methodParams = callState.Request.方法名称.Split(":"); if (methodParams.Length == 1) { method = "POST"; methodName = methodParams[0]; contentType = Constant.CONTENT_TYPE_JSON; } else if (methodParams.Length == 2) { method = methodParams[0]; methodName = methodParams[1]; contentType = Constant.CONTENT_TYPE_JSON; } else if (methodParams.Length == 3) { method = methodParams[0]; methodName = methodParams[2]; contentType = String.Compare("JSON", methodParams[1], true) == 0 ? Constant.CONTENT_TYPE_JSON : Constant.CONTENT_TYPE_XML; } //contentType = Constant.CONTENT_TYPE_JSON; //--STEP.2.根据method拼接URL if (method.ToUpper() == "GET") { message = message.Trim(); if (message.StartsWith("{"))//--如果参数以大括弧开头,说明是JSON参数,需要转化成QueryString { message = JsonToQueryString(message); } uri = uri + "/" + methodName + "?" + message; } else { uri = uri + "/" + methodName; } //uri = "http://10.100.20.180:8016/" + methodName + "?format=json"; //uri = "http://localhost:8080/" + methodName;// +"?format=json"; //--STEP.3.构造HTTP请求并调用RESTful服务 ESB.Core.Schema.务响应 response = new ESB.Core.Schema.务响应(); try { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri); webRequest.Accept = contentType; webRequest.Method = method; webRequest.ContentType = contentType; webRequest.Timeout = callState.ServiceConfig.Timeout; webRequest.Headers.Add(Constant.ESB_HEAD_TRACE_CONTEXT, callState.TraceContext.ToString()); //--STEP.3.1.如果是POST请求,则需要将消息内容发送出去 if (method == "POST" && !String.IsNullOrEmpty(message)) { byte[] data = System.Text.Encoding.UTF8.GetBytes(message); using (Stream stream = webRequest.GetRequestStream()) { stream.Write(data, 0, data.Length); } } //--STEP.3.2.获取到响应消息 callState.CallBeginTime = DateTime.Now; HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); //--从返回头消息中取到服务调用的时间 callState.ServiceBeginTime = webResponse.Headers[Constant.ESB_HEAD_SERVICE_BEGIN]; callState.ServiceEndTime = webResponse.Headers[Constant.ESB_HEAD_SERVICE_END]; using (Stream newstream = webResponse.GetResponseStream()) { using (StreamReader srRead = new StreamReader(newstream, System.Text.Encoding.UTF8)) { String outString = srRead.ReadToEnd(); callState.CallEndTime = DateTime.Now; response.消息内容 = outString; srRead.Close(); } } //--STEP.3.3.记录日志并返回ESB响应 LogUtil.AddAuditLog( 1 , binding , callState , response.消息内容, request); } catch (Exception ex) { callState.CallEndTime = DateTime.Now; String exMessage = String.Empty; if (ex.InnerException != null) { exMessage = ex.InnerException.Message; } else { exMessage = ex.Message; } throw LogUtil.ExceptionAndLog(callState, "调用目标服务抛出异常", exMessage, binding, request); } return(response); }
/// <summary> /// 利用反射调用目标服务 /// </summary> /// <param name="url"></param> /// <param name="request"></param> /// <returns></returns> internal static ESB.Core.Schema.务响应 CallWebService(CallState state) { CallState callState = state as CallState; BindingTemplate binding = callState.Binding; ESB.Core.Schema.务请求 request = callState.Request; SoapClientItem soapClient = null; try { soapClient = SoapClientCache.GetItem(binding.Address, request.务名称); } catch (Exception ex) { throw LogUtil.ExceptionAndLog(callState, "获取目标服务的代理程序集时发生异常", ex.Message, binding, request); } if (soapClient != null) { MethodInfo method = soapClient.GetMethodInfo(request.方法名称); if (method == null) { throw LogUtil.ExceptionAndLog(callState, "目标服务未实现方法", request.方法名称, binding, request); } Object req = GetRequest(soapClient, request); if (req == null) { throw LogUtil.ExceptionAndLog(callState, "无法将请求转换成目标服务所需要的输入参数", "", binding, request); } Object res = null; try { callState.CallBeginTime = DateTime.Now; res = method.Invoke(soapClient.PortObject, new Object[] { req }); callState.CallEndTime = DateTime.Now; } catch (Exception ex) { callState.CallEndTime = DateTime.Now; String message = String.Empty; if (ex.InnerException != null) { message = ex.InnerException.Message; } else { message = ex.Message; } throw LogUtil.ExceptionAndLog(callState, "调用目标服务抛出异常", message, binding, request); } ESB.Core.Schema.务响应 response = GetResponse(res, method.ReturnType); LogUtil.AddAuditLog( 1 , binding , callState.RequestBeginTime, callState.RequestEndTime, callState.CallBeginTime, callState.CallEndTime , response.消息内容, request); return(response); } else { throw LogUtil.ExceptionAndLog(callState, "获取目标服务的代理程序集失败!", "", binding, request); } }
/// <summary> /// 调用Handler服务 /// </summary> /// <param name="callState"></param> /// <returns></returns> public static ESB.Core.Schema.务响应 CallAshxService(CallState callState) { //--STEP.1.从CallState中获取到需要的信息 ESB.Core.Schema.务请求 request = callState.Request; String message = callState.Request.消息内容; BindingTemplate binding = callState.Binding; String uri = callState.Binding.Address; String contentType = String.Equals(callState.Request.消息编码, "XML", StringComparison.OrdinalIgnoreCase) ? Constant.CONTENT_TYPE_XML : Constant.CONTENT_TYPE_JSON; callState.Request.方法名称 = GetMethodName(callState.Request.方法名称); //--STEP.2.根据method拼接URL if (String.IsNullOrEmpty(callState.Request.方法名称)) { throw LogUtil.ExceptionAndLog(callState, "ASHX服务的方法名称必须填写", "", binding, request); } //--STEP.3.构造HTTP请求并调用ASHX服务 ESB.Core.Schema.务响应 response = new ESB.Core.Schema.务响应(); try { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri); webRequest.Accept = contentType; webRequest.Method = "POST"; webRequest.ContentType = contentType; webRequest.Timeout = callState.ServiceConfig.Timeout; webRequest.Headers.Add("Accept-Encoding", "gzip"); webRequest.Headers.Add(Constant.ESB_HEAD_TRACE_CONTEXT, callState.TraceContext.ToString()); webRequest.Headers.Add(Constant.ESB_HEAD_ANVOKE_ACTION, callState.Request.方法名称); //--STEP.3.1.如果是POST请求,则需要将消息内容发送出去 if (!String.IsNullOrEmpty(message)) { byte[] data = System.Text.Encoding.UTF8.GetBytes(message); using (Stream stream = webRequest.GetRequestStream()) { stream.Write(data, 0, data.Length); } } //--STEP.3.2.获取到响应消息 callState.CallBeginTime = DateTime.Now; HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); String contentEncoding = webResponse.ContentEncoding.ToLower(); callState.ServiceBeginTime = webResponse.Headers[Constant.ESB_HEAD_SERVICE_BEGIN]; callState.ServiceEndTime = webResponse.Headers[Constant.ESB_HEAD_SERVICE_END]; if (String.IsNullOrEmpty(contentEncoding)) { using (Stream newstream = webResponse.GetResponseStream()) { using (StreamReader srRead = new StreamReader(newstream, System.Text.Encoding.UTF8)) { String outString = srRead.ReadToEnd(); callState.CallEndTime = DateTime.Now; response.消息内容 = outString; srRead.Close(); } } } else if (contentEncoding.Contains("gzip")) { using (GZipStream stream = new GZipStream(webResponse.GetResponseStream(), CompressionMode.Decompress)) { using (StreamReader srRead = new StreamReader(stream, System.Text.Encoding.UTF8)) { String outString = srRead.ReadToEnd(); callState.CallEndTime = DateTime.Now; response.消息内容 = outString; srRead.Close(); } } } else if (contentEncoding.Contains("deflate")) { using (DeflateStream stream = new DeflateStream(webResponse.GetResponseStream(), CompressionMode.Decompress)) { using (StreamReader srRead = new StreamReader(stream, System.Text.Encoding.UTF8)) { String outString = srRead.ReadToEnd(); callState.CallEndTime = DateTime.Now; response.消息内容 = outString; srRead.Close(); } } } else { using (Stream newstream = webResponse.GetResponseStream()) { using (StreamReader srRead = new StreamReader(newstream, System.Text.Encoding.UTF8)) { String outString = srRead.ReadToEnd(); callState.CallEndTime = DateTime.Now; response.消息内容 = outString; srRead.Close(); } } } //--STEP.3.3.记录日志并返回ESB响应 LogUtil.AddAuditLog( 1 , binding , callState , response.消息内容, request); } catch (Exception ex) { callState.CallEndTime = DateTime.Now; String exMessage = String.Empty; if (ex.InnerException != null) { exMessage = ex.InnerException.Message; } else { exMessage = ex.Message; } throw LogUtil.ExceptionAndLog(callState, "调用目标服务抛出异常", exMessage, binding, request); } return(response); }
public static ESB.Core.Schema.务响应 CallWcfService(CallState callState) { //Console.WriteLine("CallWcfService 开始:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); //--STEP.1.从CallState中获取到需要的信息 ESB.Core.Schema.务请求 request = callState.Request; String message = callState.Request.消息内容; BindingTemplate binding = callState.Binding; String uri = callState.Binding.Address; callState.Request.方法名称 = GetMethodName(callState.Request.方法名称); //--STEP.3.构造HTTP请求并调用ASHX服务 ESB.Core.Schema.务响应 response = new ESB.Core.Schema.务响应(); try { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri); //webRequest.Proxy = null; webRequest.Method = "POST"; webRequest.ContentType = "text/xml; charset=utf-8"; webRequest.Timeout = callState.ServiceConfig.Timeout; webRequest.Headers.Add("Accept-Encoding", "gzip, deflate"); webRequest.Headers.Add(Constant.ESB_HEAD_TRACE_CONTEXT, callState.TraceContext.ToString()); webRequest.Headers.Add(Constant.ESB_HEAD_ANVOKE_ACTION, callState.Request.方法名称); if (String.IsNullOrEmpty(callState.Request.方法名称)) { throw LogUtil.ExceptionAndLog(callState, "WCF-HTTP服务的方法名称必须填写", "", binding, request); } else { webRequest.Headers.Add("SOAPAction", String.Format("{0}/EsbAction", Constant.COMPANY_URL)); } //Console.WriteLine("WebRequest.Create 完成:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); //--STEP.3.1.如果是POST请求,则需要将消息内容发送出去 if (!String.IsNullOrEmpty(message)) { String reqMessage = CommonUtil.XmlEncoding(request.消息内容); String esbAction = request.方法名称; String soapMessage = String.Format(SOAP_MESSAGE_TEMPLATE, Constant.COMPANY_URL, reqMessage); byte[] data = System.Text.Encoding.UTF8.GetBytes(soapMessage); //Console.WriteLine("webRequest.GetRequestStream 开始:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); using (Stream stream = webRequest.GetRequestStream()) { stream.Write(data, 0, data.Length); } //Console.WriteLine("webRequest.GetRequestStream 完成:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); } //--STEP.3.2.获取到响应消息 callState.CallBeginTime = DateTime.Now; HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); //--从返回头消息中取到服务调用的时间 callState.ServiceBeginTime = webResponse.Headers[Constant.ESB_HEAD_SERVICE_BEGIN]; callState.ServiceEndTime = webResponse.Headers[Constant.ESB_HEAD_SERVICE_END]; //Console.WriteLine("webRequest.GetResponse 完成:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); if (webResponse.ContentEncoding.ToLower().Contains("gzip")) { using (GZipStream stream = new GZipStream(webResponse.GetResponseStream(), CompressionMode.Decompress)) { using (StreamReader srRead = new StreamReader(stream, System.Text.Encoding.UTF8)) { String outString = srRead.ReadToEnd(); callState.CallEndTime = DateTime.Now; response.消息内容 = GetMessageFromSOAP(outString, "//" + callState.Request.方法名称 + "Result"); srRead.Close(); } } } else if (webResponse.ContentEncoding.ToLower().Contains("deflate")) { using (DeflateStream stream = new DeflateStream(webResponse.GetResponseStream(), CompressionMode.Decompress)) { using (StreamReader srRead = new StreamReader(stream, System.Text.Encoding.UTF8)) { String outString = srRead.ReadToEnd(); callState.CallEndTime = DateTime.Now; response.消息内容 = GetMessageFromSOAP(outString, "//" + callState.Request.方法名称 + "Result"); srRead.Close(); } } } else { using (Stream newstream = webResponse.GetResponseStream()) { using (StreamReader srRead = new StreamReader(newstream, System.Text.Encoding.UTF8)) { String outString = srRead.ReadToEnd(); callState.CallEndTime = DateTime.Now; response.消息内容 = GetMessageFromSOAP(outString, "//" + callState.Request.方法名称 + "Result"); srRead.Close(); } } } //Console.WriteLine("CallWcfService 完成:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); //Console.WriteLine("AddAuditLog 开始:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); //--STEP.3.3.记录日志并返回ESB响应 LogUtil.AddAuditLog( 1 , binding , callState , response.消息内容, request); //Console.WriteLine("AddAuditLog 完成:{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); } catch (Exception ex) { callState.CallEndTime = DateTime.Now; String exMessage = String.Empty; if (ex.InnerException != null) { exMessage = ex.InnerException.Message; } else { exMessage = ex.Message; } throw LogUtil.ExceptionAndLog(callState, "调用目标服务抛出异常", exMessage, binding, request); } return(response); }
public static ESB.Core.Schema.务响应 CallWcfService(CallState callState) { //--STEP.1.从CallState中获取到需要的信息 ESB.Core.Schema.务请求 request = callState.Request; String message = callState.Request.消息内容; BindingTemplate binding = callState.Binding; String uri = callState.Binding.Address; //--STEP.3.构造HTTP请求并调用ASHX服务 ESB.Core.Schema.务响应 response = new ESB.Core.Schema.务响应(); try { HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri); webRequest.Method = "POST"; webRequest.ContentType = "text/xml"; if (String.IsNullOrEmpty(callState.Request.方法名称)) { throw LogUtil.ExceptionAndLog(callState, "WCF-HTTP服务的方法名称必须填写", "", binding, request); } else { webRequest.Headers.Add("SOAPAction", String.Format("{0}/{1}", EsbClient.COMPANY_URL, request.方法名称)); } //--STEP.3.1.如果是POST请求,则需要将消息内容发送出去 if (!String.IsNullOrEmpty(message)) { String reqMessage = CommonUtil.XmlEncoding(request.消息内容); String soapMessage = String.Format(SOAP_MESSAGE_TEMPLATE, EsbClient.COMPANY_URL, reqMessage); byte[] data = System.Text.Encoding.Default.GetBytes(soapMessage); using (Stream stream = webRequest.GetRequestStream()) { stream.Write(data, 0, data.Length); } } //--STEP.3.2.获取到响应消息 callState.CallBeginTime = DateTime.Now; HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); using (Stream newstream = webResponse.GetResponseStream()) { using (StreamReader srRead = new StreamReader(newstream, System.Text.Encoding.Default)) { String outString = srRead.ReadToEnd(); callState.CallEndTime = DateTime.Now; response.消息内容 = GetMessageFromSOAP(outString, "//" + callState.Request.方法名称 + "Result"); srRead.Close(); } } //--STEP.3.3.记录日志并返回ESB响应 LogUtil.AddAuditLog( 1 , binding , callState.RequestBeginTime, callState.RequestEndTime, callState.CallBeginTime, callState.CallEndTime , response.消息内容, request); } catch (Exception ex) { callState.CallEndTime = DateTime.Now; String exMessage = String.Empty; if (ex.InnerException != null) { exMessage = ex.InnerException.Message; } else { exMessage = ex.Message; } throw LogUtil.ExceptionAndLog(callState, "调用目标服务抛出异常", exMessage, binding, request); } return(response); }