public static void RequestAsy <TRequest>(RequestModel <TRequest> request) { try { HttpSOAClient client = new HttpSOAClient(); RequestInfo model = client.GetList().Find(a => a.RequestType == request.Header.RequestType); if (model != null) { string requestStr = string.Empty; if (request.Body != null) { if (request.Body.GetType().IsClass&& request.Body.GetType() != typeof(string)) { requestStr = JsonConvert.SerializeObject(request.Body); } else { requestStr = request.Body.ToString(); } } if (requestStr.Length <= UInt16.MaxValue) { LogManager.CreateTpoLog().Info(request.Header.RequestType, requestStr); } client.RequsetAsy(model.RequestType, requestStr, model.RequestUrl, model.TimeOut); } } catch (Exception ex) { LogManager.CreateTpoLog().Error(request.Header.RequestType, ex); } }
/// <summary> /// 得到访问对象 /// </summary> public static RequestInfo GetDefaultList(string requestType) { HttpSOAClient client = new HttpSOAClient(); var data = client.GetList(); var model = data.Find(a => a.RequestType.Trim() == requestType.Trim()); if (model == null) { Esmart.Framework.Logging.LogManager.CreateTpoLog().Info(requestType, JsonConvert.SerializeObject(data));//日记记录 } return(model); }
/// <summary> /// 调用soa服务 /// </summary> /// <typeparam name="TRequest">请求对象</typeparam> /// <typeparam name="TResponse">返回对象</typeparam> /// <param name="request">请求参数</param> /// <returns></returns> public static ResponseModel <TResponse> Request <TRequest, TResponse>(RequestModel <TRequest> request) { string response = null; try { HttpSOAClient client = new HttpSOAClient(); if (request.Header.AppID == 0) { request.Header.AppID = ConstantDefine.AppID; } ResponseModel <TResponse> ReturnResponse = null; RequestInfo model = null; if (ConstantDefine.NotUsePlatForm) { model = new RequestInfo() { RequestType = request.Header.RequestType.Trim(), TimeOut = 60000, RequestUrl = string.Format("http://{0}/WebRequest.asmx", GlobalConfig.SoaDomain.Trim()) }; } else { model = GetDefaultList(request.Header.RequestType); } if (model == null) { throw new Exception("请开发人员去公共平台配置基础信息"); } if (model != null) { TResponse resonse = GetCache <TResponse>(model); if (resonse != null && !string.IsNullOrEmpty(model.GetCacheKey)) { return(new ResponseModel <TResponse>() { Body = resonse }); } string requestStr = string.Empty; if (request.Body != null) { if (request.Body.GetType().IsClass&& request.Body.GetType() != typeof(string)) { requestStr = JsonConvert.SerializeObject(request.Body); } else { requestStr = request.Body.ToString(); } } if (!string.IsNullOrEmpty(requestStr) && requestStr.Length <= UInt16.MaxValue) { LogManager.CreateTpoLog().Info(request.Header.GUID ?? request.Header.RequestType, requestStr); } response = client.Request(model.RequestType, requestStr, model.RequestUrl, model.TimeOut); ReturnResponse = JsonConvert.DeserializeObject <ResponseModel <TResponse> >(response); SetCache <TResponse>(model, ReturnResponse.Body); ClearCache(model); return(ReturnResponse); } } catch (Exception ex) { try { ResponseModel <TResponse> model = new ResponseModel <TResponse>(); if (!string.IsNullOrEmpty(response)) { var returnobj = JsonConvert.DeserializeObject <ResponseModel <object> >(response); model.Header = returnobj.Header; } else { model.Header.ReturnCode = -1; model.Header.Message = ex.Message; } return(model); } catch (Exception ex1) { ResponseModel <TResponse> model = new ResponseModel <TResponse>(); model.Header.ReturnCode = -1; model.Header.Message = ex1.Message; return(model); } } throw new TpoBaseException("程序出现未知异常,请联系 开发人员"); }