private void AddMessageHeader(IContextChannel channel, string cmd, bool iscompressjson, bool isencryptionjson, SerializeType serializetype, SysLoginRight loginright, Action callback) { using (var scope = new OperationContextScope(channel as IContextChannel)) { if (string.IsNullOrEmpty(cmd)) { cmd = ""; } HeaderParameter para = new HeaderParameter(); para.cmd = cmd; para.routerid = clientObj.RouterID; para.pluginname = clientObj.PluginName; //ReplyIdentify如果客户端创建连接为空,如果中间件连接上级中间件那就是本地中间件标识 para.replyidentify = null; para.beginidentify = BeginIdentify; para.endidentify = EndIdentify; para.token = loginright != null ? loginright.token : null; //clientObj.Token; para.iscompressjson = iscompressjson; para.isencryptionjson = isencryptionjson; para.serializetype = serializetype; para.LoginRight = loginright; para.NodePath = null; HeaderOperater.AddMessageHeader(OperationContext.Current.OutgoingMessageHeaders, para); callback(); } }
//路径执行到下一个节点 private static string PathNextRequest(string plugin, string controller, string method, string jsondata, HeaderParameter para) { string retJson = null; try { para.NodePath.NextStep(); //节点路径下一步 if (para.NodePath.IsEndMNode) //到达终节点 { //执行本地数据请求 retJson = LocalDataRequest(plugin, controller, method, jsondata, para); } else if (para.NodePath.nextMNodeDirection == "<up>")//向上 { ClientLink clientlink = SuperClient.CreateDataClient(); using (var scope = new OperationContextScope(clientlink.ClientObj.WcfService.InnerDuplexChannel as IContextChannel)) { HeaderOperater.AddMessageHeader(OperationContext.Current.OutgoingMessageHeaders, para); retJson = clientlink.ClientObj.WcfService.ProcessRequest(clientlink.ClientObj.ClientID, plugin, controller, method, jsondata); } } else if (para.NodePath.nextMNodeDirection == "<down>")//向下 { IDataReply dataReply = SuperClient.CreateDataReply(para.NodePath.nextMNode); retJson = dataReply.ReplyProcessRequest(para, plugin, controller, method, jsondata); } return(retJson); } catch (Exception err) { throw err; } }
/// <summary> /// 截获从Client端发送的消息转发到目标终结点并获得返回值给Client端 /// </summary> /// <param name="requestMessage"></param> /// <returns></returns> public Message ProcessMessage(Message requestMessage) { try { begintime(); IRouterService proxy = null; HeaderParameter para = HeaderOperater.GetHeaderValue(requestMessage); if (RouterServerManage.routerDic.ContainsKey(para.routerid)) { proxy = RouterServerManage.routerDic[para.routerid]; para.replyidentify = RouterServerManage.headParaDic[para.routerid].replyidentify; } else { //Binding binding = null; EndpointAddress endpointAddress = null; Uri touri = null; para = RouterServerManage.AddClient(requestMessage, para, out endpointAddress, out touri); requestMessage.Headers.To = touri; IDuplexRouterCallback callback = OperationContext.Current.GetCallbackChannel <IDuplexRouterCallback>(); NetTcpBinding tbinding = new NetTcpBinding("NetTcpBinding_WCFHandlerService"); DuplexChannelFactory <IRouterService> factory = new DuplexChannelFactory <IRouterService>(new InstanceContext(new DuplexRouterCallback(callback)), tbinding, endpointAddress); proxy = factory.CreateChannel(); //缓存会话 RouterServerManage.routerDic.Add(para.routerid, proxy); RouterServerManage.headParaDic.Add(para.routerid, para); } Message responseMessage = null; HeaderOperater.AddMessageHeader(requestMessage, para);//增加自定义消息头 responseMessage = proxy.ProcessMessage(requestMessage); if (para.cmd == "Quit") { //关闭连接释放缓存会话 RouterServerManage.RemoveClient(para); } double outtime = endtime(); // 请求消息记录 if (WcfServerManage.IsDebug) { RouterServerManage.hostwcfMsg(Color.Black, DateTime.Now, String.Format("路由请求消息发送(耗时[" + outtime + "]): {0}", requestMessage.Headers.Action)); } return(responseMessage); } catch (Exception e) { return(Message.CreateMessage(requestMessage.Version, FaultCode.CreateReceiverFaultCode("error", RouterServerManage.ns), e.Message, requestMessage.Headers.Action)); } }
private void AddMessageHeader(IContextChannel channel, string cmd, bool iscompressjson, bool isencryptionjson, SerializeType serializetype, Action callback) { using (var scope = new OperationContextScope(channel as IContextChannel)) { if (string.IsNullOrEmpty(cmd)) { cmd = ""; } HeaderParameter para = new HeaderParameter(); para.cmd = cmd; para.routerid = mConn.RouterID; para.pluginname = mConn.PluginName; //ReplyIdentify如果客户端创建连接为空,如果中间件连接上级中间件那就是本地中间件标识 para.replyidentify = WcfServerManage.Identify; para.token = mConn.Token; para.iscompressjson = iscompressjson; para.isencryptionjson = isencryptionjson; para.serializetype = serializetype; HeaderOperater.AddMessageHeader(OperationContext.Current.OutgoingMessageHeaders, para); callback(); } }
public Message ProcessMessage(Message requestMessage) { try { //计时器 Stopwatch sw = new Stopwatch(); sw.Start(); IRouterBaseHandler proxy = null; HeaderParameter para = HeaderOperater.GetHeaderValue(requestMessage); if (RouterManage.routerDic.ContainsKey(para.routerid)) { proxy = RouterManage.routerDic[para.routerid]; para.replyidentify = RouterManage.headParaDic[para.routerid].replyidentify; } else { //Binding binding = null; EndpointAddress endpointAddress = null; Uri touri = null; para = RouterManage.AddClient(requestMessage, para, out endpointAddress, out touri); requestMessage.Headers.To = touri; IRouterBaseReply callback = OperationContext.Current.GetCallbackChannel <IRouterBaseReply>(); NetTcpBinding tbinding = new NetTcpBinding("NetTcpBinding_BaseService"); DuplexChannelFactory <IRouterBaseHandler> factory = new DuplexChannelFactory <IRouterBaseHandler>(new InstanceContext(new ReplyRouterBaseCallback(callback)), tbinding, endpointAddress); proxy = factory.CreateChannel(); //缓存会话 RouterManage.routerDic.Add(para.routerid, proxy); RouterManage.headParaDic.Add(para.routerid, para); } Message responseMessage = null; try { HeaderOperater.AddMessageHeader(requestMessage, para);//增加自定义消息头 responseMessage = proxy.ProcessMessage(requestMessage); } catch (CommunicationException e) { RouterManage.RemoveClient(para); throw e; } if (para.cmd == "Quit") { //关闭连接释放缓存会话 RouterManage.RemoveClient(para); } //double outtime = endtime(); // 请求消息记录 if (WcfGlobal.IsDebug) { MiddlewareLogHelper.WriterLog(LogType.MidLog, true, Color.Black, String.Format("路由请求消息发送(耗时[" + sw.Elapsed.TotalMilliseconds + "]): {0}", requestMessage.Headers.Action)); } return(responseMessage); } catch (Exception e) { return(Message.CreateMessage(requestMessage.Version, FaultCode.CreateReceiverFaultCode("error", RouterManage.ns), e.Message, requestMessage.Headers.Action)); } }