Example #1
0
        public void AddOrUpdate(string token, ClientRequestData clientData)
        {
            Func <string, ClientRequestData> addValue = (key) => { return(clientData); };
            Func <string, ClientRequestData, ClientRequestData> updateValue = (key, clientRequestData) => { return(clientData); };

            clientRequestHistory.AddOrUpdate(token, addValue, updateValue);
        }
Example #2
0
        public void Verify_False_RequestsPerTimespanFailAndTimespanPassedSinceLastCallPass()
        {
            // arrange
            var clientToken       = "abc123";
            var requestDate       = new DateTime(2020, 1, 1, 0, 1, 0); // 1/1/2020 12:01AM
            var lastUpdateDate    = new DateTime(2020, 1, 1, 0, 0, 0); // 1/1/2020 12:00AM
            var lastClientRequest = new ClientRequestData(0, lastUpdateDate);

            var fakeRepository = Substitute.For <IClientRepository>();

            fakeRepository.GetClientData(clientToken).Returns(lastClientRequest);

            var fakeRateLimiterAlgorithm = Substitute.For <IRateLimiterAlgorithm>();

            fakeRateLimiterAlgorithm.VerifyRequestsPerTimeSpan(0, 5, 5, 60, requestDate, requestDate).ReturnsForAnyArgs(false);
            fakeRateLimiterAlgorithm.VerifyTimespanPassedSinceLastCall(requestDate, new TimeSpan(0, 1, 0), requestDate).ReturnsForAnyArgs(true);

            var rateLimiter = new RateLimiter(fakeRepository, fakeRateLimiterAlgorithm);

            // act
            var result = rateLimiter.Verify(clientToken, requestDate, defaultSettingsConfig);

            // assert
            Assert.AreEqual(result, false);
        }
Example #3
0
        private void OnClientDataReceived(ClientRequestData dataReceived)
        {
            if (dataReceived == null)
            {
                return;
            }

            if (this.ClientDataReceived != null)
            {
                this.ClientDataReceived(this, new ClientReadEventData(dataReceived));
            }
        }
Example #4
0
        public bool Verify(string token, DateTime requestDate, RateLimitSettingsConfig rateLimitSettingsConfig)
        {
            var isAllowed = true;
            ClientRequestData clientData = new ClientRequestData(-1, DateTime.MinValue);

            // check requests per timespan rule
            if (rateLimitSettingsConfig[RateLimitType.RequestsPerTimespan] != null)
            {
                var rateLimitSettings = (TokenBucketSettings)rateLimitSettingsConfig[RateLimitType.RequestsPerTimespan];

                clientData = this.clientRepository.GetClientData(token);
                isAllowed  = this.rateLimiterAlgorithm.VerifyRequestsPerTimeSpan(clientData.Count, rateLimitSettings.MaxAmount, rateLimitSettings.RefillAmount, rateLimitSettings.RefillTime, requestDate, clientData.LastUpdateDate);
            }

            if (!isAllowed)
            {
                return(false);
            }

            // check timespan passed
            if (rateLimitSettingsConfig[RateLimitType.TimespanPassedSinceLastCall] != null)
            {
                var rateLimitSettings = (TimespanPassedSinceLastCallSettings)rateLimitSettingsConfig[RateLimitType.TimespanPassedSinceLastCall];

                if (clientData.Count == -1)
                {
                    clientData = this.clientRepository.GetClientData(token);
                }

                isAllowed = this.rateLimiterAlgorithm.VerifyTimespanPassedSinceLastCall(requestDate, rateLimitSettings.TimespanLimit, clientData.LastUpdateDate);
            }

            if (!isAllowed)
            {
                return(false);
            }

            // update client data
            clientData.LastUpdateDate = DateTime.Now;
            this.clientRepository.AddOrUpdate(token, clientData);

            return(true);
        }
Example #5
0
 public ClientReadEventData(ClientRequestData data)
 {
     this.Data = data;
 }
Example #6
0
        /// <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 (clientObj == 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); //压缩传入参数
                }
                if (requestData.Isencryptionjson)                 //开启加密
                {
                    DESEncryptor des = new DESEncryptor();
                    des.InputString = jsondata;
                    des.DesEncrypt();
                    jsondata = des.OutString;
                }

                DuplexBaseServiceClient _wcfService = clientObj.WcfService;
                IAsyncResult            result      = null;

                AddMessageHeader(_wcfService.InnerDuplexChannel as IContextChannel, "", requestData.Iscompressjson, requestData.Isencryptionjson, requestData.Serializetype, requestData.LoginRight, (() =>
                {
                    AsyncCallback callback = delegate(IAsyncResult r)
                    {
                        string retJson = _wcfService.EndProcessRequest(r);

                        if (requestData.Isencryptionjson)//解密结果
                        {
                            DESEncryptor des = new DESEncryptor();
                            des.InputString = retJson;
                            des.DesDecrypt();
                            retJson = des.OutString;
                        }
                        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(clientObj.ClientID, clientObj.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连接服务主机失败,请联系管理员!");
            }
        }
Example #7
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 (clientObj == null)
            {
                throw new Exception("还没有创建连接!");
            }
            while (baseServiceClient.State == CommunicationState.Opening || clientObj.ClientID == null)//解决并发问题
            {
                Thread.Sleep(400);
            }

            if (baseServiceClient.State == CommunicationState.Closed || baseServiceClient.State == CommunicationState.Faulted)
            {
                ReConnection(true);//连接服务主机失败,重连
            }
            try
            {
                ClientRequestData requestData = new ClientRequestData(serverConfig.IsCompressJson, serverConfig.IsEncryptionJson, (SerializeType)serverConfig.SerializeType);
                if (requestAction != null)
                {
                    requestAction(requestData);
                }

                string jsondata = requestData.GetJsonData();      //获取序列化的请求数据

                if (requestData.Iscompressjson)                   //开启压缩
                {
                    jsondata = ZipComporessor.Compress(jsondata); //压缩传入参数
                }
                if (requestData.Isencryptionjson)                 //开启加密
                {
                    DESEncryptor des = new DESEncryptor();
                    des.InputString = jsondata;
                    des.DesEncrypt();
                    jsondata = des.OutString;
                }
                string retJson = "";
#if ClientProxy
                BaseServiceClient _wcfService = clientObj.WcfService;
                AddMessageHeader(_wcfService.InnerChannel as IContextChannel, "", requestData.Iscompressjson, requestData.Isencryptionjson, requestData.Serializetype, requestData.LoginRight, (() =>
                {
                    retJson = _wcfService.ProcessRequest(clientObj.ClientID, clientObj.PluginName, controller, method, jsondata);
                }));
#else
                DuplexBaseServiceClient _wcfService = clientObj.WcfService;
                AddMessageHeader(_wcfService.InnerDuplexChannel as IContextChannel, "", requestData.Iscompressjson, requestData.Isencryptionjson, requestData.Serializetype, requestData.LoginRight, (() =>
                {
                    retJson = _wcfService.ProcessRequest(clientObj.ClientID, clientObj.PluginName, controller, method, jsondata);
                }));
#endif
                if (requestData.Isencryptionjson)//解密结果
                {
                    DESEncryptor des = new DESEncryptor();
                    des.InputString = retJson;
                    des.DesDecrypt();
                    retJson = des.OutString;
                }
                if (requestData.Iscompressjson)//解压结果
                {
                    retJson = ZipComporessor.Decompress(retJson);
                }

                new Action(delegate()
                {
                    if (serverConfig.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连接服务主机失败,请联系管理员!");
                throw new Exception(e.Message);
            }
        }
Example #8
0
        public static string ProcessRequest(string clientId, string plugin, string controller, string method, string jsondata, HeaderParameter para)
        {
            string retJson = null;

            try
            {
                if (plugin == null || controller == null)
                {
                    throw new Exception("插件名称或控制器名称不能为空!");
                }

                if (ClientManage.ClientDic.ContainsKey(clientId) == false)
                {
                    throw new Exception("客户端不存在,正在创建新的连接!");
                }

                if (ClientManage.IsToken == true)//非调试模式下才验证
                {
                    //验证身份,创建连接的时候验证,请求不验证
                    IsAuth(plugin, controller, method, para.token);
                }

                //显示调试信息
                if (WcfGlobal.IsDebug == true)
                {
                    ShowHostMsg(Color.Black, DateTime.Now, "客户端[" + clientId + "]正在执行:" + controller + "." + method + "(" + jsondata + ")");
                }


                begintime();

                #region 执行插件控制器的核心算法
                object[]            paramValue  = null;//jsondata?
                ServiceResponseData retObj      = null;
                LocalPlugin         localPlugin = RemotePluginManage.GetLocalPlugin();
                if (string.IsNullOrEmpty(para.replyidentify) || localPlugin.ServerIdentify == para.replyidentify)
                {
                    if (localPlugin.PluginDic.ContainsKey(plugin) == true)
                    {
                        //先解密再解压
                        string _jsondata = jsondata;
                        //解密参数
                        if (para.isencryptionjson)
                        {
                            DESEncryptor des = new DESEncryptor();
                            des.InputString = _jsondata;
                            des.DesDecrypt();
                            _jsondata = des.OutString;
                        }
                        //解压参数
                        if (para.iscompressjson)
                        {
                            _jsondata = ZipComporessor.Decompress(_jsondata);
                        }

                        ClientRequestData requestData = new ClientRequestData(para.iscompressjson, para.isencryptionjson, para.serializetype);
                        requestData.SetJsonData(_jsondata);
                        requestData.LoginRight = para.LoginRight;

                        EFWCoreLib.CoreFrame.Plugin.ModulePlugin moduleplugin = localPlugin.PluginDic[plugin];
                        retObj = (ServiceResponseData)moduleplugin.WcfServerExecuteMethod(controller, method, paramValue, requestData);

                        if (retObj != null)
                        {
                            retJson = retObj.GetJsonData();
                        }
                        else
                        {
                            retObj = new ServiceResponseData();
                            retObj.Iscompressjson   = para.iscompressjson;
                            retObj.Isencryptionjson = para.isencryptionjson;
                            retObj.Serializetype    = para.serializetype;

                            retJson = retObj.GetJsonData();
                        }

                        retJson = "{\"flag\":0,\"msg\":" + "\"\"" + ",\"data\":" + retJson + "}";
                        //先压缩再加密
                        //压缩结果
                        if (para.iscompressjson)
                        {
                            retJson = ZipComporessor.Compress(retJson);
                        }
                        //加密结果
                        if (para.isencryptionjson)
                        {
                            DESEncryptor des = new DESEncryptor();
                            des.InputString = retJson;
                            des.DesEncrypt();
                            retJson = des.OutString;
                        }
                    }
                    else
                    {
                        throw new Exception("本地插件找不到指定的插件");
                    }
                }
                else//本地插件找不到,就执行远程插件
                {
                    if (RemotePluginManage.GetRemotePlugin().FindIndex(x => x.ServerIdentify == para.replyidentify) > -1)
                    {
                        RemotePlugin rp = RemotePluginManage.GetRemotePlugin().Find(x => x.ServerIdentify == para.replyidentify);
                        string[]     ps = rp.plugin;

                        if (ps.ToList().FindIndex(x => x == plugin) > -1)
                        {
                            retJson = rp.callback.ReplyProcessRequest(para, plugin, controller, method, jsondata);
                        }
                        else
                        {
                            throw new Exception("远程插件找不到指定的插件");
                        }
                    }
                    else
                    {
                        throw new Exception("远程插件找不到指定的回调中间件");
                    }
                }
                #endregion

                double outtime = endtime();
                //记录超时的方法
                if (ClientManage.IsOverTime == true)
                {
                    if (outtime > Convert.ToDouble(ClientManage.OverTime * 1000))
                    {
                        WriterOverTimeLog(outtime, controller + "." + method + "(" + jsondata + ")");
                    }
                }
                //显示调试信息
                if (WcfGlobal.IsDebug == true)
                {
                    ShowHostMsg(Color.Green, DateTime.Now, "客户端[" + clientId + "]收到结果(耗时[" + outtime + "]):" + retJson);
                }

                //更新客户端信息
                ClientManage.UpdateRequestClient(clientId, jsondata == null ? 0 : jsondata.Length, retJson == null ? 0 : retJson.Length);


                if (retJson == null)
                {
                    throw new Exception("插件执行未返回有效数据");
                }

                return(retJson);
            }
            catch (Exception err)
            {
                //记录错误日志
                if (err.InnerException == null)
                {
                    retJson = "{\"flag\":1,\"msg\":" + "\"" + err.Message + "\"" + "}";
                    if (para.iscompressjson)
                    {
                        retJson = ZipComporessor.Compress(retJson);
                    }
                    ShowHostMsg(Color.Red, DateTime.Now, "客户端[" + clientId + "]执行失败:" + err.Message);
                    return(retJson);
                }
                else
                {
                    retJson = "{\"flag\":1,\"msg\":" + "\"" + err.InnerException.Message + "\"" + "}";
                    if (para.iscompressjson)
                    {
                        retJson = ZipComporessor.Compress(retJson);
                    }
                    ShowHostMsg(Color.Red, DateTime.Now, "客户端[" + clientId + "]执行失败:" + err.InnerException.Message);
                    return(retJson);
                }
            }
        }
Example #9
0
        public static string ReplyProcessRequest(string plugin, string controller, string method, string jsondata, HeaderParameter para)
        {
            string retJson = null;

            try
            {
                //显示调试信息
                if (WcfGlobal.IsDebug == true)
                {
                    ShowHostMsg(Color.Black, DateTime.Now, "客户端[本地回调]正在执行:" + controller + "." + method + "(" + jsondata + ")");
                }

                begintime();

                #region 执行插件控制器的核心算法
                object[]            paramValue  = null;//jsondata?
                ServiceResponseData retObj      = null;
                LocalPlugin         localPlugin = RemotePluginManage.GetLocalPlugin();
                if (string.IsNullOrEmpty(para.replyidentify) || localPlugin.ServerIdentify == para.replyidentify)
                {
                    if (localPlugin.PluginDic.ContainsKey(plugin) == true)
                    {
                        //先解密再解压
                        string _jsondata = jsondata;
                        //解密参数
                        if (para.isencryptionjson)
                        {
                            DESEncryptor des = new DESEncryptor();
                            des.InputString = _jsondata;
                            des.DesDecrypt();
                            _jsondata = des.OutString;
                        }
                        //解压参数
                        if (para.iscompressjson)
                        {
                            _jsondata = ZipComporessor.Decompress(_jsondata);
                        }

                        ClientRequestData requestData = new ClientRequestData(para.iscompressjson, para.isencryptionjson, para.serializetype);
                        requestData.SetJsonData(_jsondata);
                        requestData.LoginRight = para.LoginRight;

                        EFWCoreLib.CoreFrame.Plugin.ModulePlugin moduleplugin = localPlugin.PluginDic[plugin];
                        retObj = (ServiceResponseData)moduleplugin.WcfServerExecuteMethod(controller, method, paramValue, requestData);

                        if (retObj != null)
                        {
                            retJson = retObj.GetJsonData();
                        }
                        else
                        {
                            retObj = new ServiceResponseData();
                            retObj.Iscompressjson   = para.iscompressjson;
                            retObj.Isencryptionjson = para.isencryptionjson;
                            retObj.Serializetype    = para.serializetype;

                            retJson = retObj.GetJsonData();
                        }

                        retJson = "{\"flag\":0,\"msg\":" + "\"\"" + ",\"data\":" + retJson + "}";
                        //先压缩再加密
                        //压缩结果
                        if (para.iscompressjson)
                        {
                            retJson = ZipComporessor.Compress(retJson);
                        }
                        //加密结果
                        if (para.isencryptionjson)
                        {
                            DESEncryptor des = new DESEncryptor();
                            des.InputString = retJson;
                            des.DesEncrypt();
                            retJson = des.OutString;
                        }
                    }
                    else
                    {
                        throw new Exception("本地插件找不到指定的插件");
                    }
                }
                else//本地插件找不到,就执行远程插件
                {
                    if (RemotePluginManage.GetRemotePlugin().FindIndex(x => x.ServerIdentify == para.replyidentify) > -1)
                    {
                        RemotePlugin rp = RemotePluginManage.GetRemotePlugin().Find(x => x.ServerIdentify == para.replyidentify);
                        string[]     ps = rp.plugin;

                        if (ps.ToList().FindIndex(x => x == plugin) > -1)
                        {
                            retJson = rp.callback.ReplyProcessRequest(para, plugin, controller, method, jsondata);
                        }
                        else
                        {
                            throw new Exception("远程插件找不到指定的插件");
                        }
                    }
                    else
                    {
                        throw new Exception("远程插件找不到指定的回调中间件");
                    }
                }
                #endregion

                //System.Threading.Thread.Sleep(20000);//测试并发问题,此处也没有问题
                double outtime = endtime();
                //记录超时的方法
                if (ClientManage.IsOverTime == true)
                {
                    if (outtime > Convert.ToDouble(ClientManage.OverTime * 1000))
                    {
                        WriterOverTimeLog(outtime, controller + "." + method + "(" + jsondata + ")");
                    }
                }
                //显示调试信息
                if (WcfGlobal.IsDebug == true)
                {
                    ShowHostMsg(Color.Green, DateTime.Now, "客户端[本地回调]收到结果(耗时[" + outtime + "]):" + retJson);
                }

                if (retJson == null)
                {
                    throw new Exception("请求的插件未获取到有效数据");
                }

                return(retJson);
            }
            catch (Exception err)
            {
                //记录错误日志
                //EFWCoreLib.CoreFrame.EntLib.ZhyContainer.CreateException().HandleException(err, "HISPolicy");

                if (err.InnerException == null)
                {
                    retJson = "{\"flag\":1,\"msg\":" + "\"" + err.Message + "\"" + "}";
                    if (para.iscompressjson)
                    {
                        retJson = ZipComporessor.Compress(retJson);
                    }
                    ShowHostMsg(Color.Red, DateTime.Now, "客户端[本地回调]执行失败:" + err.Message);
                    return(retJson);
                }
                else
                {
                    retJson = "{\"flag\":1,\"msg\":" + "\"" + err.InnerException.Message + "\"" + "}";
                    if (para.iscompressjson)
                    {
                        retJson = ZipComporessor.Compress(retJson);
                    }
                    ShowHostMsg(Color.Red, DateTime.Now, "客户端[本地回调]执行失败:" + err.InnerException.Message);
                    return(retJson);
                }
            }
        }
Example #10
0
        //执行服务核心方法
        private static string ExecuteService(string plugin, string controller, string method, string jsondata, HeaderParameter para)
        {
            string retJson = null;

            try
            {
                #region 执行插件控制器的核心算法
                object[]            paramValue = null;//jsondata?
                ServiceResponseData retObj     = null;

                //先解密再解压
                string _jsondata = jsondata;
                //解密参数
                if (para.isencryptionjson)
                {
                    DESEncryptor des = new DESEncryptor();
                    des.InputString = _jsondata;
                    des.DesDecrypt();
                    _jsondata = des.OutString;
                }
                //解压参数
                if (para.iscompressjson)
                {
                    _jsondata = ZipComporessor.Decompress(_jsondata);
                }

                ClientRequestData requestData = new ClientRequestData(para.iscompressjson, para.isencryptionjson, para.serializetype);
                requestData.SetJsonData(_jsondata);
                requestData.LoginRight = para.LoginRight;
                //获取插件服务
                EFWCoreLib.CoreFrame.Plugin.ModulePlugin moduleplugin = CoreFrame.Init.AppPluginManage.PluginDic[plugin];
                retObj = (ServiceResponseData)moduleplugin.WcfServerExecuteMethod(controller, method, paramValue, requestData);

                if (retObj != null)
                {
                    retJson = retObj.GetJsonData();
                }
                else
                {
                    retObj = new ServiceResponseData();
                    retObj.Iscompressjson   = para.iscompressjson;
                    retObj.Isencryptionjson = para.isencryptionjson;
                    retObj.Serializetype    = para.serializetype;

                    retJson = retObj.GetJsonData();
                }

                retJson = "{\"flag\":0,\"msg\":" + "\"\"" + ",\"data\":" + retJson + "}";
                //先压缩再加密
                //压缩结果
                if (para.iscompressjson)
                {
                    retJson = ZipComporessor.Compress(retJson);
                }
                //加密结果
                if (para.isencryptionjson)
                {
                    DESEncryptor des = new DESEncryptor();
                    des.InputString = retJson;
                    des.DesEncrypt();
                    retJson = des.OutString;
                }


                #endregion

                return(retJson);
            }
            catch (Exception err)
            {
                throw err;
            }
        }
Example #11
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连接服务主机失败,请联系管理员!");
            }
        }
        public Object WcfServerExecuteMethod(string controllername, string methodname, object[] paramValue, ClientRequestData requestData, SysLoginRight loginRight)
        {
            EFWCoreLib.WcfFrame.ServerController.WcfServerController wscontroller = helper.CreateController(plugin.name, controllername) as EFWCoreLib.WcfFrame.ServerController.WcfServerController;
            wscontroller.requestData  = requestData;
            wscontroller.responseData = new ServiceResponseData(requestData.Iscompressjson, requestData.Isencryptionjson, requestData.Serializetype);
            wscontroller.BindLoginRight(loginRight);
            MethodInfo methodinfo = helper.CreateMethodInfo(plugin.name, controllername, methodname, wscontroller);

            return(methodinfo.Invoke(wscontroller, paramValue));
        }
Example #13
0
        /// <summary>
        /// 执行WCF服务,返回结果
        /// </summary>
        /// <param name="mp"></param>
        /// <param name="controllername"></param>
        /// <param name="methodname"></param>
        /// <param name="paramValue"></param>
        /// <param name="requestData"></param>
        /// <returns></returns>
        public static Object WcfServerExecuteMethod(this ModulePlugin mp, string controllername, string methodname, object[] paramValue, ClientRequestData requestData)
        {
            if (mp.helper == null)
            {
                mp.helper = new WcfFrame.ServerController.ControllerHelper();
            }
            EFWCoreLib.WcfFrame.ServerController.WcfServerController wscontroller = mp.helper.CreateController(mp.plugin.name, controllername) as EFWCoreLib.WcfFrame.ServerController.WcfServerController;
            wscontroller.requestData  = requestData;
            wscontroller.responseData = new ServiceResponseData(requestData.Iscompressjson, requestData.Isencryptionjson, requestData.Serializetype);
            wscontroller.BindLoginRight(requestData.LoginRight);
            MethodInfo methodinfo = mp.helper.CreateMethodInfo(mp.plugin.name, controllername, methodname, wscontroller);

            return(methodinfo.Invoke(wscontroller, paramValue));
        }