예제 #1
0
        public void InvokeMethod(HttpContext context, PrepareRusult prepareResult, ApiController api, string inputString, List <object> paraList = null)
        {
            var    method     = prepareResult.MethodDescriptor;
            var    resp       = prepareResult.Response;
            string methodName = method.Name;

            if (paraList == null)
            {
                paraList = new List <object>();

                JObject jobject;
                if (!string.IsNullOrEmpty(inputString))
                {
                    jobject = (JObject)JsonConvert.DeserializeObject(inputString);
                }
                else
                {
                    jobject = null;
                }

                foreach (var item in method.Params)
                {
                    try
                    {
                        JToken token;
                        if (jobject == null || !jobject.TryGetValue(item.Name, out token))
                        {
                            if (item.Def.CanNull)
                            {
                                paraList.Add(null);
                            }
                            else
                            {
                                prepareResult.Response.code   = ApiException.CODE_ARG_ERROR;
                                prepareResult.Response.reason = "缺少参数";
                                return;
                            }
                        }
                        else
                        {
                            paraList.Add(token.ToObject(item.ParameterInfo.ParameterType));
                        }
                    }
                    catch
                    {
                        prepareResult.Response.code   = ApiException.CODE_ARG_ERROR;
                        prepareResult.Response.reason = string.Format("参数格式不正确:{0}", item.Name);
                        return;
                    }
                }
            }

            object ret;

            try
            {
                ret = method.MethodInfo.Invoke(api, paraList.ToArray());
            }
            catch (TargetInvocationException ex)
            {
                var apiEx = ex.InnerException as ApiException;
                if (apiEx != null)
                {
                    resp.code   = apiEx.Code;
                    resp.reason = apiEx.Message;
                }
                else if (ex.InnerException != null)
                {
                    resp.code   = ApiException.CODE_ERROR;
                    resp.reason = ex.InnerException.Message;
                    api.InvokeUnhandledException(context, methodName, inputString, ex.InnerException);
                    if (ApiManager.IsDebug)
                    {
                        resp.stacktrace = ex.InnerException.Message + " " + ex.InnerException.StackTrace;
                    }
                }
                else
                {
                    resp.code   = ApiException.CODE_ERROR;
                    resp.reason = string.Format("发生未知错误 method=", methodName);
                }
                return;
            }

            if (!prepareResult.ResponseVoid)
            {
                method.ResponseSetResultMethod.Invoke(resp, new object[] { ret });
            }

            if (api.TempUser != null)
            {
                resp.User = api.TempUser;
            }
            if (resp.User == null)
            {
                resp.User = new Identity();
            }
        }
예제 #2
0
        private PrepareRusult PrepareMethod(HttpContext context, ApiController api, string methodName)
        {
            var result = new PrepareRusult();

            MethodDescriptor method;
            if (!_apiDescriptor.MethodsDic.TryGetValue(methodName, out method))
            {
                result.Response = new Response { code = ApiException.CODE_MISS_METHOD, reason = "方法不存在" };
                return result;
            }
            result.MethodDescriptor = method;

            bool respVoid = method.ResponseParameterInfo.ParameterType == typeof(void);
            result.ResponseVoid = respVoid;

            if (respVoid)
            {
                result.Response = new Response();
            }
            else
            {
                result.Response = (Response)Activator.CreateInstance(method.ResponseParam.Type);
            }

            var resp = result.Response;
            Identity user = null;
            if (IsDebug)
            {
                string uid = context.Request.QueryString["user"];
                if (!string.IsNullOrEmpty(uid))
                {
                    user = new Identity(uid, "debug");
                    api.Authetication.SaveUser(context, user);
                }
            }

            if (user == null)
                user = api.Authetication.GetUser(context);

            if (user == null || !user.IsAuthenticated)
            {
                try
                {
                    user = api.Authetication.VerifyUser(context);
                    if (user != null)
                        api.Authetication.SaveUser(context, user);
                }
                catch (Exception ex)
                {
                    resp.code = ApiException.CODE_ERROR;
                    resp.reason = "验证时发生未知错误";
                    api.InvokeUnhandledException(context, methodName, null, ex);
                    if (ApiManager.IsDebug)
                        resp.stacktrace = ex.Message + " " + ex.StackTrace;
                    result.Response = resp;
                    return result;
                }
            }

            if (method.NeedAuth && (user == null || !user.IsAuthenticated))
            {
                resp.code = ApiException.CODE_UNAUTH;
                resp.reason = "没有权限";
                result.Response = resp;
                result.Response.redirect = context.Response.RedirectLocation;
                return result;
            }

            if (user == null)
            {
                user = new Identity();
                api.TempUser = user;
            }

            resp.User = user;
            result.Response = resp;

            return result;
        }
예제 #3
0
        private PrepareRusult PrepareMethod(HttpContext context, ApiController api, string methodName)
        {
            var result = new PrepareRusult();

            MethodDescriptor method;

            if (!_apiDescriptor.MethodsDic.TryGetValue(methodName, out method))
            {
                result.Response = new Response {
                    code = ApiException.CODE_MISS_METHOD, reason = "方法不存在"
                };
                return(result);
            }
            result.MethodDescriptor = method;

            bool respVoid = method.ResponseParameterInfo.ParameterType == typeof(void);

            result.ResponseVoid = respVoid;

            if (respVoid)
            {
                result.Response = new Response();
            }
            else
            {
                result.Response = (Response)Activator.CreateInstance(method.ResponseParam.Type);
            }

            var      resp = result.Response;
            Identity user = null;

            if (IsDebug)
            {
                string uid = context.Request.QueryString["user"];
                if (!string.IsNullOrEmpty(uid))
                {
                    user = new Identity(uid, "debug");
                    api.Authetication.SaveUser(context, user);
                }
            }

            if (user == null)
            {
                user = api.Authetication.GetUser(context);
            }

            if (user == null || !user.IsAuthenticated)
            {
                try
                {
                    user = api.Authetication.VerifyUser(context);
                    if (user != null)
                    {
                        api.Authetication.SaveUser(context, user);
                    }
                }
                catch (Exception ex)
                {
                    resp.code   = ApiException.CODE_ERROR;
                    resp.reason = "验证时发生未知错误";
                    api.InvokeUnhandledException(context, methodName, null, ex);
                    if (ApiManager.IsDebug)
                    {
                        resp.stacktrace = ex.Message + " " + ex.StackTrace;
                    }
                    result.Response = resp;
                    return(result);
                }
            }

            if (method.NeedAuth && (user == null || !user.IsAuthenticated))
            {
                resp.code                = ApiException.CODE_UNAUTH;
                resp.reason              = "没有权限";
                result.Response          = resp;
                result.Response.redirect = context.Response.RedirectLocation;
                return(result);
            }

            if (user == null)
            {
                user         = new Identity();
                api.TempUser = user;
            }

            resp.User       = user;
            result.Response = resp;

            return(result);
        }
예제 #4
0
        public void InvokeMethod(HttpContext context, PrepareRusult prepareResult, ApiController api, string inputString, List<object> paraList = null)
        {
            var method = prepareResult.MethodDescriptor;
            var resp = prepareResult.Response;
            string methodName = method.Name;

            if (paraList == null)
            {
                paraList = new List<object>();

                JObject jobject;
                if (!string.IsNullOrEmpty(inputString))
                    jobject = (JObject)JsonConvert.DeserializeObject(inputString);
                else
                    jobject = null;

                foreach (var item in method.Params)
                {
                    try
                    {
                        JToken token;
                        if (jobject == null || !jobject.TryGetValue(item.Name, out token))
                        {
                            if (item.Def.CanNull) paraList.Add(null);
                            else
                            {
                                prepareResult.Response.code = ApiException.CODE_ARG_ERROR;
                                prepareResult.Response.reason = "缺少参数";
                                return;
                            }
                        }
                        else
                        {
                            paraList.Add(token.ToObject(item.ParameterInfo.ParameterType));
                        }
                    }
                    catch
                    {
                        prepareResult.Response.code = ApiException.CODE_ARG_ERROR;
                        prepareResult.Response.reason = string.Format("参数格式不正确:{0}", item.Name);
                        return;
                    }
                }
            }

            object ret;
            try
            {
                ret = method.MethodInfo.Invoke(api, paraList.ToArray());
            }
            catch (TargetInvocationException ex)
            {
                var apiEx = ex.InnerException as ApiException;
                if (apiEx != null)
                {
                    resp.code = apiEx.Code;
                    resp.reason = apiEx.Message;
                }
                else if (ex.InnerException != null)
                {
                    resp.code = ApiException.CODE_ERROR;
                    resp.reason = ex.InnerException.Message;
                    api.InvokeUnhandledException(context, methodName, inputString, ex.InnerException);
                    if (ApiManager.IsDebug)
                        resp.stacktrace = ex.InnerException.Message + " " + ex.InnerException.StackTrace;
                }
                else
                {
                    resp.code = ApiException.CODE_ERROR;
                    resp.reason = string.Format("发生未知错误 method=", methodName);
                }
                return;
            }

            if (!prepareResult.ResponseVoid)
            {
                method.ResponseSetResultMethod.Invoke(resp, new object[] { ret });
            }

            if (api.TempUser != null) resp.User = api.TempUser;
                if (resp.User == null) resp.User = new Identity();
        }