private static void AddClient(string sessionId, string clientId, string ipaddress, DateTime time, IClientService clientService) { WCFClientInfo info = new WCFClientInfo(); info.clientId = clientId; info.ipAddress = ipaddress; info.startTime = time; info.clientServiceCallBack = clientService; info.IsConnect = true; lock (wcfClientDic) { wcfClientDic.Add(clientId, info); } ShowHostMsg(Color.Blue, DateTime.Now, "客户端[" + ipaddress + "]已连接WCF服务主机"); }
private static void AddClient(string sessionId, string clientId, string ipaddress, DateTime time, IClientService clientService, string plugin, string replyidentify) { WCFClientInfo info = new WCFClientInfo(); info.clientId = clientId; info.ipAddress = ipaddress; info.startTime = time; info.callbackClient = clientService; info.IsConnect = true; info.plugin = plugin; info.ServerIdentify = replyidentify; lock (wcfClientDic) { wcfClientDic.Add(clientId, info); } ShowHostMsg(Color.Blue, DateTime.Now, "客户端[" + ipaddress + "]已连接WCF服务主机"); }
private static void AddClient(string sessionId, string clientId, string ipaddress, DateTime time, IClientService clientService) { WCFClientInfo info = new WCFClientInfo(); info.clientId = clientId; info.ipAddress = ipaddress; info.startTime = time; info.clientServiceCallBack = clientService; info.IsConnect = true; lock (wcfClientDic) { wcfClientDic.Add(clientId, info); } ShowHostMsg(DateTime.Now, "客户端[" + ipaddress + "]已连接WCF服务主机"); hostwcfclientinfoList(wcfClientDic.Values.ToList()); }
//每次请求的身份验证,分布式情况下验证麻烦 static bool IsAuth(string pname, string cname, string methodname, string token, WCFClientInfo clientinfo) { ModulePlugin mp; WcfControllerAttributeInfo cattr = AppPluginManage.GetPluginWcfControllerAttributeInfo(pname, cname, out mp); if (cattr == null) { throw new Exception("插件中没有此控制器名"); } WcfMethodAttributeInfo mattr = cattr.MethodList.Find(x => x.methodName == methodname); if (mattr == null) { throw new Exception("控制器中没有此方法名"); } if (mattr.IsAuthentication) { if (token == null) { throw new Exception("no token"); } AuthResult result = SsoHelper.ValidateToken(token); if (result.ErrorMsg != null) { throw new Exception(result.ErrorMsg); } SysLoginRight loginInfo = new SysLoginRight(); loginInfo.UserId = Convert.ToInt32(result.User.UserId); loginInfo.EmpName = result.User.UserName; clientinfo.LoginRight = loginInfo; } return(true); }
public static string ProcessRequest(string clientId, string plugin, string controller, string method, string jsondata, HeaderParameter para) { string retJson = null; WCFClientInfo ClientInfo = null; try { if (plugin == null || controller == null) { throw new Exception("插件名称或控制器名称不能为空!"); } lock (wcfClientDic) { if (wcfClientDic.ContainsKey(clientId) == false) { throw new Exception("客户端不存在,正在创建新的连接!"); } ClientInfo = wcfClientDic[clientId].Clone() as WCFClientInfo; } if (WcfServerManage.IsDebug == false)//非调试模式下才验证 { //验证身份,创建连接的时候验证,请求不验证 //IsAuth(plugin, controller, method, token, ClientInfo); } //显示调试信息 if (WcfServerManage.IsDebug == true) { ShowHostMsg(Color.Black, DateTime.Now, "客户端[" + clientId + "]正在执行:" + controller + "." + method + "(" + jsondata + ")"); } begintime(); #region 执行插件控制器的核心算法 object[] paramValue = null;//jsondata? ServiceResponseData retObj = null; if (string.IsNullOrEmpty(para.replyidentify) || localPlugin.ServerIdentify == para.replyidentify) { if (localPlugin.PluginDic.ContainsKey(plugin) == true) { //解压参数 string _jsondata = jsondata; if (para.iscompressjson) { _jsondata = ZipComporessor.Decompress(jsondata); } ClientRequestData requestData = new ClientRequestData(para.iscompressjson, para.isencryptionjson, para.serializetype); requestData.SetJsonData(_jsondata); EFWCoreLib.CoreFrame.Plugin.ModulePlugin moduleplugin = localPlugin.PluginDic[plugin]; retObj = (ServiceResponseData)moduleplugin.WcfServerExecuteMethod(controller, method, paramValue, requestData, ClientInfo.LoginRight); 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); } } else { throw new Exception("本地插件找不到指定的插件"); } } else//本地插件找不到,就执行远程插件 { if (RemotePluginDic.FindIndex(x => x.ServerIdentify == para.replyidentify) > -1) { RemotePlugin rp = RemotePluginDic.Find(x => x.ServerIdentify == para.replyidentify); string[] ps = rp.plugin; if (ps.ToList().FindIndex(x => x == plugin) > -1) { retJson = rp.clientService.SuperReplyClient(para, plugin, controller, method, jsondata); } else { throw new Exception("远程插件找不到指定的插件"); } } else { throw new Exception("远程插件找不到指定的回调中间件"); } } #endregion double outtime = endtime(); //记录超时的方法 if (WcfServerManage.IsOverTime == true) { if (outtime > Convert.ToDouble(WcfServerManage.OverTime * 1000)) { WriterOverTimeLog(outtime, controller + "." + method + "(" + jsondata + ")"); } } //显示调试信息 if (WcfServerManage.IsDebug == true) { ShowHostMsg(Color.Green, DateTime.Now, "客户端[" + clientId + "]收到结果(耗时[" + outtime + "]):" + retJson); } //更新客户端信息 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); } } }
public static string ProcessRequest(string clientId, string controller, string method, string jsondata) { string retJson = null; WCFClientInfo ClientInfo = null; try { lock (wcfClientDic) { if (wcfClientDic.ContainsKey(clientId) == false) { throw new Exception("客户端不存在,正在创建新的连接!"); } ClientInfo = wcfClientDic[clientId].Clone() as WCFClientInfo; } //显示调试信息 if (WcfServerManage.IsDebug == true) { ShowHostMsg(Color.Black, DateTime.Now, "客户端[" + clientId + "]正在执行:" + controller + "." + method + "(" + jsondata + ")"); } begintime(); object[] paramValue = null;//jsondata? //解压参数 string _jsondata = jsondata; if (WcfServerManage.IsCompressJson) { _jsondata = ZipComporessor.Decompress(jsondata); //_jsondata = JsonComporessor.Decompress(jsondata); } string[] names = controller.Split(new char[] { '@' }); if (names.Length != 2) { throw new Exception("控制器名称错误!"); } string pluginname = names[0]; string cname = names[1]; Object retObj = null; if (AppPluginManage.PluginDic.ContainsKey(pluginname) == true) { EFWCoreLib.CoreFrame.Plugin.ModulePlugin plugin = AppPluginManage.PluginDic[pluginname]; retObj = plugin.WcfServerExecuteMethod(cname, method, paramValue, _jsondata, ClientInfo); } else { throw new Exception("请求的插件未找到"); } if (retObj != null) { retJson = retObj.ToString(); } retJson = "{\"flag\":0,\"msg\":" + "\"\"" + ",\"data\":" + retJson + "}"; //压缩结果 if (WcfServerManage.IsCompressJson) { retJson = ZipComporessor.Compress(retJson); //retJson = JsonComporessor.Compress(retJson); } //System.Threading.Thread.Sleep(20000);//测试并发问题,此处也没有问题 double outtime = endtime(); //记录超时的方法 if (WcfServerManage.IsOverTime == true) { if (outtime > Convert.ToDouble(WcfServerManage.OverTime * 1000)) { WriterOverTimeLog(outtime, controller + "." + method + "(" + _jsondata + ")"); } } //显示调试信息 if (WcfServerManage.IsDebug == true) { ShowHostMsg(Color.Green, DateTime.Now, "客户端[" + clientId + "]收到结果(耗时[" + outtime + "]):" + retJson); } //更新客户端信息 UpdateRequestClient(clientId, jsondata == null ? 0 : jsondata.Length, retJson == null ? 0 : retJson.Length); return(retJson); } catch (Exception err) { //记录错误日志 //EFWCoreLib.CoreFrame.EntLib.ZhyContainer.CreateException().HandleException(err, "HISPolicy"); if (err.InnerException == null) { retJson = "{\"flag\":1,\"msg\":" + "\"" + err.Message + "\"" + "}"; if (WcfServerManage.IsCompressJson) { retJson = ZipComporessor.Compress(retJson); //retJson = JsonComporessor.Compress(retJson); } ShowHostMsg(Color.Red, DateTime.Now, "客户端[" + clientId + "]执行失败:" + err.Message); return(retJson); } else { retJson = "{\"flag\":1,\"msg\":" + "\"" + err.InnerException.Message + "\"" + "}"; if (WcfServerManage.IsCompressJson) { retJson = ZipComporessor.Compress(retJson); //retJson = JsonComporessor.Compress(retJson); } ShowHostMsg(Color.Red, DateTime.Now, "客户端[" + clientId + "]执行失败:" + err.InnerException.Message); return(retJson); } } }