Esempio n. 1
0
        /// <summary>
        /// 底层传输上来的协议内容
        /// </summary>
        /// <param name="args">args</param>
        /// <param name="context">context</param>
        /// <returns>string</returns>
        public string Distributer(string args, object context)
        {
            //内部虚拟事件ID
            EventId internalEventId = new EventId(0, Guid.NewGuid().ToString("N"));

            logger.LogInformation(internalEventId, $"收到请求\r\n\targs={args}\r\n\tcontext={JsonConvert.SerializeObject(context)}");
            // 构造一个请求日志
            var requestLog = new LogRequest()
            {
                Parameters = $"参见[{internalEventId.Name}]"
            };
            StatusCode    code = StatusCode.OK;
            string        rid  = string.Empty;
            Args <object> a    = null;

            try
            {
                a = JsonConvert.DeserializeObject <Args <object> >(args, requestJsonSetting);
            }
            catch (Exception ex)
            {
                code = StatusCode.ArgesError;
                logger.LogError(internalEventId, ex, "反序列化参数[args]异常");
            }

            ComboxClass <Type, MethodInfo> tInfo = null;

            if (a == null)
            {
                code = StatusCode.ArgesError;
            }
            else
            {
                //外部事件ID(外部传入的rid)
                EventId externalEventId = new EventId(0, a.rid);
                if (!servers.TryGetValue(a.m.ToLower(), out tInfo))
                {
                    code = StatusCode.MethodNotExist;
                }
                else
                {
                    try
                    {
                        object   obj = Activator.CreateInstance(tInfo.V1);
                        object[] o   = new object[] { a, code, context };
                        object   r   = tInfo.V2.Invoke(obj, o);
                        code = (StatusCode)o[1];
                        if (r != null)
                        {
                            string rr = JsonConvert.SerializeObject(r, resultJsonSetting);
                            requestLog.Result = rr;
                            requestLog.SetInfo(a, new Result <object>()
                            {
                                c   = code.code,
                                msg = code.msg,
                            });
                            logger.LogInformation(externalEventId, $"请求处理正常结束\r\n\t{requestLog.ToString()}");
                            return(rr);
                        }
                    }
                    catch (Exception ex)
                    {
                        code     = StatusCode.ServerError;
                        code.msg = $"{ex.Message}:{ex.StackTrace}";
                        logger.LogError(externalEventId, ex, "请求处理异常结束");
                    }
                }

                rid = a.rid;
            }

            Result <object> rst = new Result <object>();

            rst.c   = code.code;
            rst.msg = code.msg.ToLower();
            string rs = JsonConvert.SerializeObject(rst, resultJsonSetting);

            logger.LogInformation(internalEventId, $"请求未处理返回:{rs}");
            return(rs);
        }