コード例 #1
0
ファイル: ESBService.cs プロジェクト: radtek/LJC.FrameWork
        protected sealed override void ReciveMessage(Message message)
        {
            if (message.IsMessage((int)SOAMessageType.DoSOATransferRequest))
            {
                SOATransferRequest request = null;
                try
                {
                    var responseMsg = new Message((int)SOAMessageType.DoSOATransferResponse);
                    responseMsg.MessageHeader.TransactionID = message.MessageHeader.TransactionID;
                    SOATransferResponse responseBody = new SOATransferResponse();
                    request = message.GetMessageBody <SOATransferRequest>();
                    responseBody.ClientTransactionID = request.ClientTransactionID;
                    responseBody.ClientId            = request.ClientId;

                    if (SocketApplicationEnvironment.TraceMessage)
                    {
                        LogHelper.Instance.Debug(string.Format("接收服务请求,请求号:{0}", request.ClientTransactionID));
                    }

                    try
                    {
                        var result = DoResponse(request.FundId, request.Param, request.ClientId);
                        responseBody.Result    = LJC.FrameWork.EntityBuf.EntityBufCore.Serialize(result);
                        responseBody.IsSuccess = true;

                        if (SocketApplicationEnvironment.TraceMessage)
                        {
                            LogHelper.Instance.Debug(string.Format("处理请求:请求号:{0},客户端请求号:{1},服务号:{2},功能号:{3},结果:{4},序列化结果:{5}",
                                                                   responseMsg.MessageHeader.TransactionID, request.ClientTransactionID, ServiceNo, request.FundId, Comm.JsonUtil <object> .Serialize(result), Convert.ToBase64String(responseBody.Result)));
                        }
                    }
                    catch (Exception ex)
                    {
                        responseBody.IsSuccess = false;
                        responseBody.ErrMsg    = ex.Message;

                        LogHelper.Instance.Error(string.Format("服务转发出错,请求号:{0},服务号:{1},功能号:{2}",
                                                               request.ClientTransactionID, ServiceNo, request.FundId), ex);
                    }

                    responseMsg.SetMessageBody(responseBody);

                    this.SendMessage(responseMsg);

                    return;
                }
                catch (Exception ex)
                {
                    LogHelper.Instance.Error(string.Format("服务转发出错,请求号:{0},服务号:{1},功能号:{2}",
                                                           request == null ? "0" : request.ClientTransactionID,
                                                           ServiceNo, request == null ? 0 : request.FundId), ex);

                    return;
                }
            }

            base.ReciveMessage(message);
        }
コード例 #2
0
ファイル: ESBServer.cs プロジェクト: radtek/LJC.FrameWork
        internal void DoTransferRequest(Session session, string msgTransactionID, SOARequest request)
        {
            session.BusinessTimeStamp = DateTime.Now;
            session.Tag = new Tuple <int, int>(request.ServiceNo, request.FuncId);

            SOAResponse resp   = new SOAResponse();
            Message     msgRet = new Message((int)SOAMessageType.DoSOAResponse);

            msgRet.MessageHeader.TransactionID = msgTransactionID;
            resp.IsSuccess = true;

            //调用本地的方法
            if (request.ServiceNo == 0)
            {
                try
                {
                    var obj = DoRequest(request.FuncId, request.Param);
                    resp.Result = EntityBuf.EntityBufCore.Serialize(obj);
                }
                catch (Exception ex)
                {
                    resp.IsSuccess = false;
                    resp.ErrMsg    = ex.Message;
                }

                msgRet.SetMessageBody(resp);
                session.SendMessage(msgRet);
            }
            else
            {
                //查询服务
                var serviceInfos = ServiceContainer.FindAll(p => p.ServiceNo.Equals(request.ServiceNo));
                if (serviceInfos == null || serviceInfos.Count == 0)
                {
                    resp.IsSuccess = false;
                    resp.ErrMsg    = string.Format("{0}服务未注册。", request.ServiceNo);
                }

                if (resp.IsSuccess)
                {
                    ESBServiceInfo serviceInfo = null;
                    if (serviceInfos.Count == 1)
                    {
                        serviceInfo = serviceInfos[0];
                    }
                    else
                    {
                        Random rd  = new Random();
                        var    idx = rd.Next(1, serviceInfos.Count + 1);
                        serviceInfo = serviceInfos[idx - 1];
                    }

                    try
                    {
                        if (DateTime.Now.Subtract(serviceInfo.Session.LastSessionTime).TotalSeconds > 30)
                        {
                            lock (LockObj)
                            {
                                ServiceContainer.Remove(serviceInfo);
                                serviceInfo.Session.Close();
                            }
                            serviceInfo = ServiceContainer.FindAll(p => p.ServiceNo.Equals(request.ServiceNo)).LastOrDefault();
                            if (serviceInfo == null)
                            {
                                throw new Exception(string.Format("{0}服务可能不可用,30秒无应答。", request.ServiceNo));
                            }
                        }

                        string             clientid        = session.SessionID;
                        SOATransferRequest transferrequest = new SOATransferRequest();
                        transferrequest.ClientId            = clientid;
                        transferrequest.FundId              = request.FuncId;
                        transferrequest.Param               = request.Param;
                        transferrequest.ClientTransactionID = msgTransactionID;

                        Message msg = new Message((int)SOAMessageType.DoSOATransferRequest);
                        msg.MessageHeader.TransactionID = SocketApplicationComm.GetSeqNum();
                        msg.SetMessageBody(transferrequest);

                        try
                        {
                            ConatinerLock.EnterWriteLock();
                            ClientSessionList.Add(msgTransactionID, session);
                        }
                        finally
                        {
                            ConatinerLock.ExitWriteLock();
                        }

                        if (serviceInfo.Session.SendMessage(msg))
                        {
                            //LogHelper.Instance.Debug(string.Format("发送SOA请求,请求序列:{0},服务号:{1},功能号:{2}",
                            //    msgTransactionID, request.ServiceNo, request.FuncId));
                            return;
                        }
                        else
                        {
                            try
                            {
                                ConatinerLock.EnterWriteLock();
                                ClientSessionList.Remove(msgTransactionID);
                            }
                            finally
                            {
                                ConatinerLock.ExitWriteLock();
                            }
                        }
                        //var result = SendMessageAnsy<byte[]>(serviceInfo.Session, msg);

                        //resp.Result = result;
                    }
                    catch (Exception ex)
                    {
                        OnError(ex);

                        resp.IsSuccess = false;
                        resp.ErrMsg    = ex.Message;
                    }
                }

                if (!resp.IsSuccess)
                {
                    msgRet.SetMessageBody(resp);
                    session.SendMessage(msgRet);
                }
            }
        }