/// <summary> /// 向服务发送异步请求 /// 客户端建议不要用多线程,都采用异步请求方式 /// </summary> /// <param name="controller">控制器名称</param> /// <param name="method">方法名称</param> /// <param name="jsondata">数据</param> /// <returns>返回Json数据</returns> public static IAsyncResult RequestAsync(string controller, string method, string jsondata, Action <string> action) { try { if (WcfClientManage.IsCompressJson) //开启压缩 { jsondata = ZipComporessor.Compress(jsondata); //压缩传入参数 //jsondata = JsonComporessor.Compress(jsondata);//压缩传入参数 } IWCFHandlerService _wcfService = AppGlobal.cache.GetData("WCFService") as IWCFHandlerService; //string retJson; IAsyncResult result = null; using (var scope = new OperationContextScope(_wcfService as IContextChannel)) { var router = System.ServiceModel.Channels.MessageHeader.CreateHeader("routerID", myNamespace, AppGlobal.cache.GetData("routerID").ToString()); OperationContext.Current.OutgoingMessageHeaders.Add(router); AsyncCallback callback = delegate(IAsyncResult r) { string retJson = _wcfService.EndProcessRequest(r); if (WcfClientManage.IsCompressJson) { retJson = ZipComporessor.Decompress(retJson); //retJson = JsonComporessor.Decompress(retJson); } action(retJson); }; result = _wcfService.BeginProcessRequest(AppGlobal.cache.GetData("WCFClientID").ToString(), controller, method, jsondata, callback, null); } if (WcfClientManage.IsHeartbeat == false)//如果没有启动心跳,则请求发送心跳 { WcfClientManage.ServerConfigRequestState = false; Heartbeat(); } //return retJson; return(result); } catch (Exception e) { WcfClientManage.ServerConfigRequestState = false; ReConnection(true);//连接服务主机失败,重连 throw new Exception(e.Message + "\n连接服务主机失败,请联系管理员!"); } }
/// <summary> /// 向服务发送请求 /// </summary> /// <param name="controller">控制器名称</param> /// <param name="method">方法名称</param> /// <param name="jsondata">数据</param> /// <returns>返回Json数据</returns> public static IAsyncResult RequestAsync(string controller, string method, string jsondata, Action <string> action) { IWCFHandlerService _wcfService = AppGlobal.cache.GetData("WCFService") as IWCFHandlerService; //string retJson; IAsyncResult result = null; using (var scope = new OperationContextScope(_wcfService as IContextChannel)) { var router = System.ServiceModel.Channels.MessageHeader.CreateHeader("routerID", myNamespace, AppGlobal.cache.GetData("routerID").ToString()); OperationContext.Current.OutgoingMessageHeaders.Add(router); AsyncCallback callback = delegate(IAsyncResult r) { string retJson = _wcfService.EndProcessRequest(r); action(retJson); }; result = _wcfService.BeginProcessRequest(AppGlobal.cache.GetData("WCFClientID").ToString(), controller, method, jsondata, callback, null); } //return retJson; return(result); }
/// <summary> /// 向服务发送异步请求 /// 客户端建议不要用多线程,都采用异步请求方式 /// </summary> /// <param name="controller">插件名@控制器名称</param> /// <param name="method">方法名称</param> /// <param name="jsondata">数据</param> /// <returns>返回Json数据</returns> public IAsyncResult RequestAsync(string controller, string method, Action <ClientRequestData> requestAction, Action <ServiceResponseData> action) { if (mConn == null) { throw new Exception("还没有创建连接!"); } try { ClientRequestData requestData = new ClientRequestData(IsCompressJson, IsEncryptionJson, serializeType); if (requestAction != null) { requestAction(requestData); } string jsondata = requestData.GetJsonData(); //获取序列化的请求数据 if (requestData.Iscompressjson) //开启压缩 { jsondata = ZipComporessor.Compress(jsondata); //压缩传入参数 } IWCFHandlerService _wcfService = mConn.WcfService; IAsyncResult result = null; AddMessageHeader(_wcfService as IContextChannel, "", requestData.Iscompressjson, requestData.Isencryptionjson, requestData.Serializetype, (() => { AsyncCallback callback = delegate(IAsyncResult r) { string retJson = _wcfService.EndProcessRequest(r); if (requestData.Iscompressjson) { retJson = ZipComporessor.Decompress(retJson); } string retData = ""; object Result = JsonConvert.DeserializeObject(retJson); int ret = Convert.ToInt32((((Newtonsoft.Json.Linq.JObject)Result)["flag"]).ToString()); string msg = (((Newtonsoft.Json.Linq.JObject)Result)["msg"]).ToString(); if (ret == 1) { throw new Exception(msg); } else { retData = ((Newtonsoft.Json.Linq.JObject)(Result))["data"].ToString(); } ServiceResponseData responsedata = new ServiceResponseData(); responsedata.Iscompressjson = requestData.Iscompressjson; responsedata.Isencryptionjson = requestData.Isencryptionjson; responsedata.Serializetype = requestData.Serializetype; responsedata.SetJsonData(retData); action(responsedata); }; result = _wcfService.BeginProcessRequest(mConn.ClientID, mConn.PluginName + "@" + controller, method, jsondata, callback, null); })); new Action(delegate() { if (IsHeartbeat == false)//如果没有启动心跳,则请求发送心跳 { ServerConfigRequestState = false; Heartbeat(); } }).BeginInvoke(null, null);//异步执行 return(result); } catch (Exception e) { ServerConfigRequestState = false; ReConnection(true);//连接服务主机失败,重连 throw new Exception(e.Message + "\n连接服务主机失败,请联系管理员!"); } }