Example #1
0
        /// <summary>
        /// 获取通用接口 -- 返回单个Common集合
        /// </summary>
        /// <param name="key">接口KEY字符串</param>
        /// <param name="skey">接口KEY数字</param>
        /// <returns>结果对象</returns>
        public static object GetCommonApi(string key, short skey = 0)
        {
            string staffNo = "";

            if (HttpContext.Current.Session["StaffNo"] != null)
            {
                staffNo = HttpContext.Current.Session["StaffNo"].ToString();
            }
            string keyCacheCode = key;

            if (key == "Menus")
            {
                keyCacheCode = key + staffNo;
            }
            Cache cache = HttpRuntime.Cache;

            if (cache.Get(keyCacheCode) != null)
            {
                return(cache.Get(keyCacheCode));
            }

            CommonResponse commonResponse = WeChatHelper.CallService <WeChat.ServiceModel.Base.Common, CommonResponse>("",
                                                                                                                       new WeChat.ServiceModel.Base.Common {
                CurrOper = staffNo, RequestType = skey
            }, ConfigurationManager.AppSettings["WeChat_CommonURI"]);

            object returnValue = commonResponse.GetType().GetProperty(key).GetValue(commonResponse, null);

            if (returnValue != null)
            {
                cache.Insert(keyCacheCode, returnValue, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, 180, 0));
            }
            return(returnValue);
        }
Example #2
0
        private async Task <T> RequestToServer <T>(AbstractNamingRequest request)
            where T : CommonResponse
        {
            try
            {
                request.PutAllHeader(GetSecurityHeaders());
                request.PutAllHeader(GetSpasHeaders(NamingUtils.GetGroupedNameOptional(request.ServiceName, request.GroupName)));

                CommonResponse response =
                    requestTimeout < 0
                        ? await rpcClient.Request(request)
                        : await rpcClient.Request(request, requestTimeout);

                if (response == null)
                {
                    throw new NacosException(NacosException.SERVER_ERROR, "Request nacos server failed: RequestToServer<T>");
                }

                if (response.ResultCode != 200)
                {
                    throw new NacosException(response.ErrorCode, response.Message);
                }

                if (response is T)
                {
                    return((T)response);
                }

                _logger?.LogError("Server return unexpected response '{0}', expected response should be '{1}'", response.GetType().Name, typeof(T).Name);
            }
            catch (NacosException e)
            {
                throw new NacosException(e.ErrorCode, $"Request nacos server failed: {e.ErrorCode}, {e.Message}");
            }
            catch (Exception e)
            {
                throw new NacosException(NacosException.SERVER_ERROR, $"Request nacos server failed: {e.Message}");
            }

            throw new NacosException(NacosException.SERVER_ERROR, "Server return invalid response");
        }