示例#1
0
        /// <summary>
        /// 处理HTTP请求的入口方法
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task <IActionResult> ProcessRequestAsync(HttpContext context)
        {
            IRequireHttpContext xx = _instance as IRequireHttpContext;

            if (xx != null)
            {
                xx.HttpContext = context;
            }


            // 构造方法的调用参数
            ParameterResolver pr = ObjectFactory.New <ParameterResolver>();

            object[] parameters = pr.GetParameters(_method, context.Request);

            // 执行方法
            object result = null;

            if (_isTaskMethod)
            {
                if (_hasReturn)
                {
                    Task  task = (Task)_method.FastInvoke(_instance, parameters);
                    await task;

                    // 从 Task<T> 中获取返回值
                    PropertyInfo property = task.GetType().GetProperty("Result", BindingFlags.Instance | BindingFlags.Public);
                    result = property.FastGetValue(task);
                }
                else
                {
                    await(Task) _method.FastInvoke(_instance, parameters);
                }
            }
            else
            {
                if (_hasReturn)
                {
                    result = _method.FastInvoke(_instance, parameters);
                }
                else
                {
                    _method.FastInvoke(_instance, parameters);
                }
            }


            // 没有执行结果,直接返回(不产生输出)
            if (result == null)
            {
                return(null);
            }


            // 转换结果
            IActionResult actionResult = result as IActionResult;

            if (actionResult == null)
            {
                ResultConvert converter = ObjectFactory.New <ResultConvert>();
                actionResult = converter.Convert(result, context);
            }

            return(actionResult);
        }