/// <summary>
        /// 向服务发送请求
        /// </summary>
        /// <param name="controller">控制器名称</param>
        /// <param name="method">方法名称</param>
        /// <param name="jsondata">数据</param>
        /// <returns>返回Json数据</returns>
        public static string Request(string controller, string method, string jsondata)
        {
            IWCFHandlerService _wcfService = AppGlobal.cache.GetData("WCFService") as IWCFHandlerService;
            string             retJson;

            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);
                retJson = _wcfService.ProcessRequest(AppGlobal.cache.GetData("WCFClientID").ToString(), controller, method, jsondata);
            }

            return(retJson);
        }
        /// <summary>
        /// 向服务发送请求
        /// </summary>
        /// <param name="controller">控制器名称</param>
        /// <param name="method">方法名称</param>
        /// <param name="jsondata">数据</param>
        /// <returns>返回Json数据</returns>
        public static string Request(string controller, string method, string jsondata)
        {
            try
            {
                if (WcfClientManage.IsCompressJson)               //开启压缩
                {
                    jsondata = ZipComporessor.Compress(jsondata); //压缩传入参数
                    //jsondata = JsonComporessor.Compress(jsondata);//压缩传入参数
                }

                IWCFHandlerService _wcfService = AppGlobal.cache.GetData("WCFService") as IWCFHandlerService;
                string             retJson;
                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);
                    retJson = _wcfService.ProcessRequest(AppGlobal.cache.GetData("WCFClientID").ToString(), controller, method, jsondata);
                }

                if (WcfClientManage.IsCompressJson)
                {
                    retJson = ZipComporessor.Decompress(retJson);
                    //retJson = JsonComporessor.Decompress(retJson);
                }

                if (WcfClientManage.IsHeartbeat == false)//如果没有启动心跳,则请求发送心跳
                {
                    WcfClientManage.ServerConfigRequestState = false;
                    Heartbeat();
                }

                return(retJson);
            }
            catch (Exception e)
            {
                WcfClientManage.ServerConfigRequestState = false;
                ReConnection(true);//连接服务主机失败,重连
                throw new Exception(e.Message + "\n连接服务主机失败,请联系管理员!");
            }
        }
Beispiel #3
0
        /// <summary>
        /// 向服务发送请求
        /// </summary>
        /// <param name="controller">控制器名称</param>
        /// <param name="method">方法名称</param>
        /// <param name="requestAction">数据</param>
        /// <returns>返回Json数据</returns>
        public ServiceResponseData Request(string controller, string method, Action <ClientRequestData> requestAction)
        {
            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;
                string             retJson     = "";

                AddMessageHeader(_wcfService as IContextChannel, "", requestData.Iscompressjson, requestData.Isencryptionjson, requestData.Serializetype, (() =>
                {
                    retJson = _wcfService.ProcessRequest(mConn.ClientID, mConn.PluginName + "@" + controller, method, jsondata);
                }));

                if (requestData.Iscompressjson)
                {
                    retJson = ZipComporessor.Decompress(retJson);
                    //retJson = JsonComporessor.Decompress(retJson);
                }

                new Action(delegate()
                {
                    if (IsHeartbeat == false)//如果没有启动心跳,则请求发送心跳
                    {
                        ServerConfigRequestState = false;
                        Heartbeat();
                    }
                }).BeginInvoke(null, null);//异步执行

                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);

                return(responsedata);
            }
            catch (Exception e)
            {
                ServerConfigRequestState = false;
                ReConnection(true);//连接服务主机失败,重连
                throw new Exception(e.Message + "\n连接服务主机失败,请联系管理员!");
            }
        }