Beispiel #1
0
        /// <summary>
        /// 获取请求信息
        /// </summary>
        /// <param name="request"></param>
        private void GetRequestData(Request request)
        {
            //- Head
            var head = request.Form["head"];

            if (head == null)
            {
                throw new Exception("Request head data not exist or format error");
            }
            head     = Encoding.UTF8.GetString(EncryptHelper.Base64Decode(head));
            HeadData = JsonConvert.DeserializeObject <RequestHead>(head);
            if (HeadData == null)
            {
                throw new ArgumentNullException("head", "Request head data not exist");
            }
            if (string.IsNullOrEmpty(HeadData.Command))
            {
                throw new ArgumentNullException("command", "Request command is null or empty");
            }

            //- Body
            var bodyForm = request.Form["body"];

            if (!string.IsNullOrWhiteSpace(bodyForm))
            {
                string key = Settings.GetSignKey(HeadData.Channel);
                bodyForm  = EncryptHelper.DESDecrypt(bodyForm, key);
                bodyForm  = Encoding.UTF8.GetString(EncryptHelper.Base64Decode(bodyForm));
                BodyDatas = JsonConvert.DeserializeObject <List <Dictionary <string, object> > >(bodyForm);
            }

            //- Route
            // Command参数如果不是json数组装换为数组处理
            if (!HeadData.Command.StartsWith("[") && !HeadData.Command.EndsWith("]"))
            {
                HeadData.Command = string.Format("[\"{0}\"]", HeadData.Command);
            }
            string[] cmds = JsonConvert.DeserializeObject <string[]>(HeadData.Command);

            if (BodyDatas != null && cmds.Count() != BodyDatas.Count)
            {
                throw new Exception("Request body number of parameters error");
            }

            Dictionary <string, RouteData> routeDatas = new Dictionary <string, RouteData>();

            foreach (var cmd in cmds)
            {
                RouteData route = RouteHelper.GetOptimalRoute(cmd, HeadData.Version, HeadData.System);
                if (route == null)
                {
                    throw new ArgumentNullException("route", "Route data not exist");
                }

                routeDatas.Add(cmd, route);
            }

            //路由负载
            OptimalRoutes = RouteHelper.RoutingLoadBalance(routeDatas);
        }
        /// <summary>
        /// 获取请求信息
        /// </summary>
        private async Task <CustomRouteData> GetRequestData(HttpRequest request)
        {
            string requestBodyStr = "";

            using (var buffer = new MemoryStream())
            {
                await Request.Body.CopyToAsync(buffer);

                requestBodyStr = Encoding.UTF8.GetString(buffer.ToArray());
            }
            requestBody = requestBodyStr;

            //Get authorization information in the request headers, it needs  send to the micro service.
            authorizationHeadValue = Request.Headers[Const.HEAD_NAME_AUTHORIZATION];

            var requestHeadStr = Request.Headers[Const.HEAD_NAME_ROUTE_INFO];
            var requestHeadDic = HttpHelper.GetFormDictionary(requestHeadStr);

            requestHead = new RequestHead {
                BusinessCode = requestHeadDic["BusinessCode"], Ttl = Convert.ToInt32(requestHeadDic.GetValueOrDefault("Ttl", "0")), Channel = requestHeadDic.GetValueOrDefault("Channel", ""), Version = requestHeadDic.GetValueOrDefault("Version", "")
            };

            CustomRouteData route = routeHelper.GetOptimalRoute(requestHead.BusinessCode, requestHead.Version, requestHead.Channel);

            if (route == null)
            {
                throw new Exception("请求路由不存在");
            }
            return(route);
        }
Beispiel #3
0
        /// <summary>
        /// 获取请求信息
        /// </summary>
        private CustomRouteData GetRequestData(Request request)
        {
            //- Head
            var head = request.Headers["head"].FirstOrDefault();

            if (head == null)
            {
                throw new Exception("请求报文头数据不存在或格式不正确");
            }
            head     = Encoding.UTF8.GetString(EncodingHelper.Base64UrlDecode(head));
            HeadData = JsonConvert.DeserializeObject <RequestHead>(head);
            if (HeadData == null)
            {
                throw new Exception("请求报文头数据不存在");
            }
            if (string.IsNullOrEmpty(HeadData.Command))
            {
                throw new Exception("请求报文头指令名称不能为空");
            }
            //- Body
            var bodyForm = request.Form["body"];

            if (!string.IsNullOrWhiteSpace(bodyForm))
            {
                string key = SettingsHelper.GetDesKey(HeadData.Channel);
                bodyForm = EncryptHelper.DESDecrypt(bodyForm, key);
                string bodyStr = Encoding.UTF8.GetString(EncodingHelper.Base64UrlDecode(bodyForm));
                BodyData = JsonConvert.DeserializeObject <Dictionary <string, object> >(bodyStr);
            }
            //- Route
            CustomRouteData route = RouteHelper.GetOptimalRoute(HeadData.Command, HeadData.Version, HeadData.System);

            if (route == null)
            {
                throw new Exception("请求路由不存在");
            }

            route = RouteHelper.RoutingLoadBalance(route);

            return(route);
        }