Example #1
0
        /// <summary>
        /// 执行,按实际结果返回
        /// </summary>
        /// <param name="letter">客户端递交的参数信息</param>
        /// <returns></returns>
        public static object Exec(Letter letter)
        {
            //1.创建对象,即$api.get("account/single")中的account
            object execObj = ExecuteMethod.CreateInstance(letter);
            //2.获取要执行的方法,即$api.get("account/single")中的single
            MethodInfo method = getMethod(execObj.GetType(), letter);

            //3#.验证方法的特性,一是验证Http动词,二是验证是否登录后操作,三是验证权限
            //----验证Http谓词访问限制
            HttpAttribute.Verify(letter.HTTP_METHOD, method);
            //LoginAttribute.Verify(method);
            //----范围控制,本机或局域网,或同域
            bool isRange = RangeAttribute.Verify(letter, method);
            //----验证是否需要登录
            LoginAttribute loginattr = LoginAttribute.Verify(method);


            //4.构建执行该方法所需要的参数
            object[] parameters = getInvokeParam(method, letter);
            //5.执行方法,返回结果
            object objResult = null;    //结果
            //只有get方式时,才使用缓存
            CacheAttribute cache = null;

            if (letter.HTTP_METHOD == "GET")
            {
                cache = CacheAttribute.GetAttr <CacheAttribute>(method);
            }
            if (cache != null)
            {
                objResult = CacheAttribute.GetResult(method, letter);
                if (objResult == null)
                {
                    objResult = method.Invoke(execObj, parameters);
                    CacheAttribute.Insert(cache.Expires, method, letter, objResult);
                }
            }
            else
            {
                objResult = method.Invoke(execObj, parameters);
            }
            //将执行结果写入日志
            LoginAttribute.LogWrite(loginattr, objResult);
            return(objResult);
        }
        private LambdaExpression GetExpressionForBodyRequest(HttpAttribute httpAttribute, Type returnType, ParameterInfo[] parameters)
        {
            var parameterExpressions = parameters
                                       .Select(p => (Expression.Parameter(p.ParameterType, p.Name), p))
                                       .ToArray();
            var bodyParameter           = GetBodyParameter(parameters);
            var bodyParameterExpression = bodyParameter == null
                ? (Expression)Expression.Constant(null)
                : parameterExpressions
                                          .Select(pe => pe.Item1)
                                          .First(pe => pe.Name == bodyParameter.Name && pe.Type == bodyParameter.ParameterType);

            return(GetExpressionForRequest(
                       httpAttribute,
                       returnType,
                       parameters,
                       parameterExpressions,
                       bodyParameterExpression));
        }
 private static void ValidateServiceMethodParameters(ParameterInfo[] parameters, HttpAttribute httpAttribute)
 {
     if (parameters.Any(p => p.GetCustomAttribute <BodyAttribute>() != null && (httpAttribute is GetAttribute || httpAttribute is DeleteAttribute)))
     {
         throw new ServiceInitializeException("GET or DELETE http request cannot has body");
     }
 }
 private LambdaExpression GetExpressionForRequest(
     HttpAttribute httpAttribute,
     Type returnType,
     ParameterInfo[] parameters,
     (ParameterExpression, ParameterInfo)[] parameterExpressions,