public override IAsyncResult BeginExecute(ControllerContext controllerContext, IDictionary <string, object> parameters, AsyncCallback callback, object state)
        {
            if (controllerContext == null)
            {
                throw new ArgumentNullException("controllerContext");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            AsyncManager    asyncHelper         = GetAsyncManager(controllerContext.Controller);
            SingleFireEvent setupCompletedEvent = new SingleFireEvent();

            BeginInvokeCallback beginCallback = (innerCallback, innerState) => {
                ManualAsyncResult asyncResult = new ManualAsyncResult()
                {
                    AsyncState = innerState
                };

                // Get parameters for async setup method, then execute
                ParameterInfo[] setupParametersInfos    = SetupMethod.GetParameters();
                var             rawSetupParameterValues = from parameterInfo in setupParametersInfos
                                                          select ExtractParameterFromDictionary(parameterInfo, parameters, SetupMethod);

                object[] setupParametersArray = rawSetupParameterValues.ToArray();

                // to simplify the logic, force an asynchronous callback
                asyncHelper.OutstandingOperations.Completed += delegate {
                    if (setupCompletedEvent.Signal())
                    {
                        ThreadPool.QueueUserWorkItem(o => {
                            asyncResult.MarkCompleted(false /* completedSynchronously */, innerCallback);
                        });
                    }
                };

                MethodDispatcher setupDispatcher = DispatcherCache.GetDispatcher(SetupMethod);
                asyncHelper.OutstandingOperations.Increment();
                setupDispatcher.Execute(controllerContext.Controller, setupParametersArray);
                asyncHelper.OutstandingOperations.Decrement();
                return(asyncResult);
            };

            AsyncCallback <object> endCallback = ar => {
                if (setupCompletedEvent.Signal())
                {
                    // the setup method did not complete before this callback executed
                    throw new InvalidOperationException(MvcResources.AsyncActionDescriptor_EndExecuteCalledPrematurely);
                }

                // Get parameters for action method, then execute
                ParameterInfo[] completionParametersInfos    = CompletionMethod.GetParameters();
                var             rawCompletionParameterValues = from parameterInfo in completionParametersInfos
                                                               select ExtractParameterOrDefaultFromDictionary(parameterInfo, asyncHelper.Parameters);

                object[] completionParametersArray = rawCompletionParameterValues.ToArray();

                MethodDispatcher completionDispatcher = DispatcherCache.GetDispatcher(CompletionMethod);
                object           actionReturnValue    = completionDispatcher.Execute(controllerContext.Controller, completionParametersArray);
                return(actionReturnValue);
            };

            // Set the timeout and go
            int timeout = asyncHelper.Timeout;

            return(AsyncResultWrapper.WrapWithTimeout(callback, state, beginCallback, endCallback, timeout, _executeTag));
        }
 public IAsyncResult ToAsyncResultWrapper(AsyncCallback callback, object state)
 {
     return(AsyncResultWrapper.Wrap <T>(callback, state, BeginInvoke, EndInvoke));
 }
 public override object EndExecute(IAsyncResult asyncResult)
 {
     return(AsyncResultWrapper.UnwrapAndContinue <object>(asyncResult, _executeTag));
 }
Exemplo n.º 4
0
 protected internal virtual ActionExecutedContext EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)
 {
     return(AsyncResultWrapper.UnwrapAndContinue <ActionExecutedContext>(asyncResult, _invokeActionMethodWithFiltersTag));
 }
Exemplo n.º 5
0
        public virtual IAsyncResult BeginInvokeAction(ControllerContext controllerContext, string actionName, AsyncCallback callback, object state)
        {
            if (controllerContext == null)
            {
                throw new ArgumentNullException("controllerContext");
            }
            if (String.IsNullOrEmpty(actionName))
            {
                throw new ArgumentException(MvcResources.Common_NullOrEmpty, "actionName");
            }

            ControllerDescriptor controllerDescriptor = GetControllerDescriptor(controllerContext);
            ActionDescriptor     actionDescriptor     = FindAction(controllerContext, controllerDescriptor, actionName);

            if (actionDescriptor == null)
            {
                // notify controller that no method matched
                return(CreateInvokeActionNotFoundAsyncResult(callback, state));
            }

            Action     continuation;
            FilterInfo filterInfo = GetFilters(controllerContext, actionDescriptor);

            try {
                AuthorizationContext authContext = InvokeAuthorizationFilters(controllerContext, filterInfo.AuthorizationFilters, actionDescriptor);
                if (authContext.Result != null)
                {
                    // the auth filter signaled that we should let it short-circuit the request
                    continuation = () => {
                        BeginInvokeActionEndContinuation(controllerContext, filterInfo.ExceptionFilters, () => {
                            InvokeActionResult(controllerContext, authContext.Result);
                        });
                    };
                }
                else
                {
                    if (controllerContext.Controller.ValidateRequest)
                    {
                        ValidateRequest(controllerContext.HttpContext.Request);
                    }

                    IDictionary <string, object> parameters = GetParameterValues(controllerContext, actionDescriptor);

                    return(AsyncResultWrapper.Wrap(callback, state,
                                                   (innerCallback, innerState) => BeginInvokeActionMethodWithFilters(controllerContext, filterInfo.ActionFilters, actionDescriptor, parameters, innerCallback, innerState),
                                                   ar => {
                        BeginInvokeActionEndContinuation(controllerContext, filterInfo.ExceptionFilters, () => {
                            ActionExecutedContext postActionContext = EndInvokeActionMethodWithFilters(ar);
                            InvokeActionResultWithFilters(controllerContext, filterInfo.ResultFilters, postActionContext.Result);
                        });
                        return true;
                    },
                                                   _invokeActionTag));
                }
            }
            catch (ThreadAbortException) {
                // This type of exception occurs as a result of Response.Redirect(), but we special-case so that
                // the filters don't see this as an error.
                throw;
            }
            catch (Exception ex) {
                // something blew up, so execute the exception filters
                ExceptionContext exceptionContext = InvokeExceptionFilters(controllerContext, filterInfo.ExceptionFilters, ex);
                if (!exceptionContext.ExceptionHandled)
                {
                    throw;
                }

                continuation = () => InvokeActionResult(controllerContext, exceptionContext.Result);
            }

            return(CreateInvokeActionContinuationAsyncResult(callback, state, continuation));
        }
Exemplo n.º 6
0
 internal static ActionExecutedContext EndInvokeActionMethodFilter(IAsyncResult asyncResult)
 {
     return(AsyncResultWrapper.UnwrapAndContinue <ActionExecutedContext>(asyncResult));
 }
Exemplo n.º 7
0
 protected internal virtual ActionResult EndInvokeActionMethod(IAsyncResult asyncResult)
 {
     return(AsyncResultWrapper.UnwrapAndContinue <ActionResult>(asyncResult, _invokeActionMethodTag));
 }
Exemplo n.º 8
0
 public virtual bool EndInvokeAction(IAsyncResult asyncResult)
 {
     return(AsyncResultWrapper.UnwrapAndContinue <bool>(asyncResult, _invokeActionTag));
 }
Exemplo n.º 9
0
 protected internal virtual void EndExecuteCore(IAsyncResult asyncResult)
 {
     AsyncResultWrapper.UnwrapAndContinue(asyncResult, _executeCoreTag);
 }
Exemplo n.º 10
0
 protected virtual void EndProcessRequest(IAsyncResult result)
 {
     AsyncResultWrapper.UnwrapAndContinue(result, _processRequestTag);
 }