示例#1
0
        internal T DoRequest <T>(int serviceno, int funcid, object param)
        {
            SOARequest request = new SOARequest();

            request.ServiceNo = serviceno;
            request.FuncId    = funcid;
            if (param == null)
            {
                request.Param = null;
            }
            else
            {
                request.Param = EntityBufCore.Serialize(param);
            }

            Message msg = new Message((int)SOAMessageType.DoSOARequest);

            msg.MessageHeader.TransactionID = SocketApplicationComm.GetSeqNum();
            msg.MessageBuffer = EntityBufCore.Serialize(request);

            T result = SendMessageAnsy <T>(msg);

            return(result);
        }
示例#2
0
        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);
                }
            }
        }