示例#1
0
        public static IHttpHandler CreateHandler(InvokeInfo vkInfo)
        {
            // 对异步的支持,只限于返回值类型是 Task 或者 Task<T>
            // 所以这里只判断是不是Task类型的返回值,如果是,则表示是一个异步Action

            if (vkInfo.Action.MethodInfo.IsTaskMethod())
            {
                return(TaskAsyncActionHandler.CreateHandler(vkInfo));
            }

            else
            {
                return(ActionHandler.CreateHandler(vkInfo));
            }
        }
示例#2
0
        internal async Task ProcessRequestAsync(HttpContext context, TaskAsyncActionHandler handler)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (handler == null)
            {
                throw new ArgumentNullException("handler");
            }

            this.HttpContext = context;
            this.Handler     = handler;
            this.InvokeInfo  = handler.InvokeInfo;

            // 设置 BaseController 的相关属性
            SetController();

            // 在异步执行前,先保存当前同步上下文的实例,供异步完成后执行切换调用。
            SynchronizationContext syncContxt = SynchronizationContext.Current;

            if (syncContxt == null)
            {
                throw new InvalidProgramException();
            }


            // 进入请求处理阶段
            ExecuteBeginRequest();

            // 安全检查
            ExecuteSecurityCheck();

            // 授权检查
            ExecuteAuthorizeRequest();

            // 执行 Action
            object actionResult = await ExecuteActionAsync();

            // 切换到原先的上下文环境,执行余下操作
            syncContxt.Send(x => {
                // 设置输出缓存
                SetOutputCache();

                // 处理方法的返回结果
                ExecuteProcessResult(actionResult);
            }, this.HttpContext);
        }