예제 #1
0
        private async Task <HttpResponseMessage> ExecuteAction(HttpActionBinding actionBinding, HttpActionContext actionContext,
                                                               CancellationToken cancellationToken, IEnumerable <IActionFilter> actionFilters, ServicesContainer controllerServices)
        {
            cancellationToken.ThrowIfCancellationRequested();
            await actionBinding.ExecuteBindingAsync(actionContext, cancellationToken);

            _modelState = actionContext.ModelState;
            cancellationToken.ThrowIfCancellationRequested();
            return(await InvokeActionWithActionFilters(actionContext, cancellationToken, actionFilters, () =>
            {
                return controllerServices.GetActionInvoker().InvokeActionAsync(actionContext, cancellationToken);
            })());
        }
예제 #2
0
        public virtual Task <HttpResponseMessage> ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)
        {
            if (_request != null)
            {
                // if user has registered a controller factory which produces the same controller instance, we should throw here
                throw Error.InvalidOperation(SRResources.CannotSupportSingletonInstance, typeof(ApiController).Name, typeof(IHttpControllerActivator).Name);
            }

            Initialize(controllerContext);

            // We can't be reused, and we know we're disposable, so make sure we go away when
            // the request has been completed.
            if (_request != null)
            {
                _request.RegisterForDispose(this);
            }

            HttpControllerDescriptor controllerDescriptor = controllerContext.ControllerDescriptor;
            ServicesContainer        controllerServices   = controllerDescriptor.Configuration.Services;
            HttpActionDescriptor     actionDescriptor     = controllerServices.GetActionSelector().SelectAction(controllerContext);
            HttpActionContext        actionContext        = new HttpActionContext(controllerContext, actionDescriptor);

            IEnumerable <FilterInfo> filters = actionDescriptor.GetFilterPipeline();

            FilterGrouping filterGrouping = new FilterGrouping(filters);

            IEnumerable <IActionFilter>        actionFilters        = filterGrouping.ActionFilters;
            IEnumerable <IAuthorizationFilter> authorizationFilters = filterGrouping.AuthorizationFilters;
            IEnumerable <IExceptionFilter>     exceptionFilters     = filterGrouping.ExceptionFilters;

            // Func<Task<HttpResponseMessage>>
            Task <HttpResponseMessage> result = InvokeActionWithAuthorizationFilters(actionContext, cancellationToken, authorizationFilters, () =>
            {
                HttpActionBinding actionBinding = actionDescriptor.ActionBinding;
                Task bindTask = actionBinding.ExecuteBindingAsync(actionContext, cancellationToken);
                return(bindTask.Then <HttpResponseMessage>(() =>
                {
                    _modelState = actionContext.ModelState;
                    Func <Task <HttpResponseMessage> > invokeFunc = InvokeActionWithActionFilters(actionContext, cancellationToken, actionFilters, () =>
                    {
                        return controllerServices.GetActionInvoker().InvokeActionAsync(actionContext, cancellationToken);
                    });
                    return invokeFunc();
                }));
            })();

            result = InvokeActionWithExceptionFilters(result, actionContext, cancellationToken, exceptionFilters);

            return(result);
        }