Example #1
0
        private static IAsyncResult ActionNotFound(AsyncCallback callback, object state)
        {
            BeginInvokeDelegate      beginDelegate = MakeSynchronousAsyncResult;
            EndInvokeDelegate <bool> endDelegate   = delegate(IAsyncResult asyncResult) {
                return(false);
            };

            return(AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, _invokeTag));
        }
        protected virtual IAsyncResult BeginProcessRequest(
            HttpContextBase context, AsyncCallback callback, object state)
        {
            AppendVersionHeader(context);
            string controllerName = Context.RouteData.GetRequiredValue <string>("controller");

            IControllerFactory factory    = Builder.GetControllerFactory();
            IController        controller = factory.CreateController(Context, controllerName);

            if (controller == null)
            {
                throw Error.CouldNotCreateController(controllerName);
            }

            IAsyncController asyncController = (controller as IAsyncController);

            if (asyncController == null)             // synchronous controller
            {
                Action action = delegate {
                    try
                    {
                        controller.Execute(Context);
                    }
                    finally
                    {
                        factory.ReleaseController(controller);
                    }
                };
                return(AsyncResultWrapper.BeginSynchronous(callback, state, action, _tag));
            }

            // asynchronous controller
            BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState) {
                try
                {
                    return(asyncController.BeginExecute(Context, asyncCallback, asyncState));
                }
                finally
                {
                    factory.ReleaseController(asyncController);
                }
            };
            EndInvokeDelegate endDelegate = delegate(IAsyncResult asyncResult) {
                try
                {
                    asyncController.EndExecute(asyncResult);
                }
                finally
                {
                    factory.ReleaseController(asyncController);
                }
            };

            return(AsyncResultWrapper.Begin(AsyncTask.WrapCallbackForSynchronizedExecution(callback,
                                                                                           SynchronizationContextExtensions.GetSynchronizationContext()),
                                            state, beginDelegate, endDelegate, _tag));
        }
        protected virtual IAsyncResult BeginExecute(RequestContext context,
                                                    AsyncCallback callback, object state)
        {
            Precondition.Require(context, () => Error.ArgumentNull("context"));

            VerifyExecuteCalledOnce();
            Initialize(context);

            return(AsyncResultWrapper.Begin(callback, state,
                                            OnBeginExecute, OnEndExecute, _executeTag));
        }
Example #4
0
        private IAsyncResult BeginInvokeAsynchronousActionMethod(ControllerContext context,
                                                                 AsyncActionDescriptor action, IDictionary <string, object> parameters,
                                                                 AsyncCallback callback, object state)
        {
            BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState) {
                return(action.BeginExecute(context, parameters, asyncCallback, asyncState));
            };
            EndInvokeDelegate <ActionResult> endDelegate = delegate(IAsyncResult asyncResult) {
                object       returnValue = action.EndExecute(asyncResult);
                ActionResult result      = CreateActionResult(context, action, returnValue);

                return(result);
            };

            return(AsyncResultWrapper.Begin(callback, state,
                                            beginDelegate, endDelegate, _invokeMethodTag));
        }
        public override IAsyncResult BeginExecute(ControllerContext context,
                                                  IDictionary <string, object> parameters, AsyncCallback callback, object state)
        {
            Precondition.Require(context, () => Error.ArgumentNull("context"));
            Precondition.Require(parameters, () => Error.ArgumentNull("parameters"));

            AsyncManager asyncManager = GetAsyncManager(context.Controller);

            BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState) {
                object[] parameterValues = _entryMethod.GetParameters()
                                           .Select(p => ExtractParameter(p, parameters, _entryMethod))
                                           .ToArray();

                TriggerListener listener    = new TriggerListener();
                MvcAsyncResult  asyncResult = new MvcAsyncResult(asyncState);

                Trigger finishTrigger = listener.CreateTrigger();
                asyncManager.Finished += delegate {
                    finishTrigger.Fire();
                };
                asyncManager.OutstandingOperations.Increment();
                listener.SetContinuation(() => ThreadPool.QueueUserWorkItem(_ =>
                                                                            asyncResult.MarkCompleted(false, asyncCallback)));

                DispatcherCache.GetDispatcher(_entryMethod)
                .Execute(context.Controller, parameterValues);

                asyncManager.OutstandingOperations.Decrement();
                listener.Activate();

                return(asyncResult);
            };

            EndInvokeDelegate <object> endDelegate = delegate(IAsyncResult asyncResult) {
                object[] parameterValues = _completedMethod.GetParameters()
                                           .Select(p => ExtractParameter(p, parameters, _completedMethod))
                                           .ToArray();

                return(DispatcherCache.GetDispatcher(_completedMethod)
                       .Execute(context.Controller, parameterValues));
            };

            return(AsyncResultWrapper.Begin(callback, state, beginDelegate,
                                            endDelegate, _executeTag, asyncManager.Timeout));
        }
        protected virtual IAsyncResult OnBeginExecute(AsyncCallback callback, object state)
        {
            LoadTempData();

            try
            {
                string actionName = RouteData.GetRequiredValue <string>("action");

                IActionExecutor      executor      = ActionExecutor;
                IAsyncActionExecutor asyncExecutor = (executor as IAsyncActionExecutor);

                if (asyncExecutor != null)
                {
                    BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState) {
                        return(asyncExecutor.BeginInvokeAction(Context, actionName, asyncCallback, asyncState, new ValueDictionary()));
                    };
                    EndInvokeDelegate endDelegate = delegate(IAsyncResult asyncResult) {
                        if (!asyncExecutor.EndInvokeAction(asyncResult))
                        {
                            HandleUnknownAction(actionName);
                        }
                    };
                    return(AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, _tag));
                }
                else
                {
                    Action action = () => {
                        if (!executor.InvokeAction(Context, actionName, null))
                        {
                            HandleUnknownAction(actionName);
                        }
                    };
                    return(AsyncResultWrapper.BeginSynchronous(callback, state, action, _tag));
                }
            }
            catch
            {
                SaveTempData();
                throw;
            }
        }
Example #7
0
        protected virtual IAsyncResult BeginInvokeActionFilters(ControllerContext context,
                                                                ActionDescriptor action, ICollection <IActionFilter> filters, IDictionary <string, object> parameters,
                                                                AsyncCallback callback, object state)
        {
            Func <ActionExecutedContext> endContinuation = null;

            BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState) {
                ActionExecutionContext preContext       = new ActionExecutionContext(context, action);
                IAsyncResult           innerAsyncResult = null;

                Func <Func <ActionExecutedContext> > beginContinuation = () => {
                    innerAsyncResult = BeginInvokeActionMethod(context, action, parameters, asyncCallback, asyncState);
                    return(() => new ActionExecutedContext(preContext, null)
                    {
                        Result = EndInvokeActionMethod(innerAsyncResult)
                    });
                };
                Func <Func <ActionExecutedContext> > thunk = filters.Reverse().Aggregate(beginContinuation,
                                                                                         (next, filter) => () => InvokeActionFilterAsynchronously(filter, preContext, next));
                endContinuation = thunk();

                if (innerAsyncResult != null)
                {
                    return(innerAsyncResult);
                }
                else
                {
                    MvcAsyncResult newAsyncResult = new MvcAsyncResult(asyncState);
                    newAsyncResult.MarkCompleted(true, asyncCallback);

                    return(newAsyncResult);
                }
            };
            EndInvokeDelegate <ActionExecutedContext> endDelegate = delegate(IAsyncResult asyncResult) {
                return(endContinuation());
            };

            return(AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, _invokeMethodFiltersTag));
        }
Example #8
0
        protected virtual IAsyncResult BeginProcessRequest(
            HttpContextBase context, AsyncCallback callback, object state)
        {
            IHttpHandler      handler      = GetHttpHandler(context);
            IHttpAsyncHandler asyncHandler = (handler as IHttpAsyncHandler);

            if (asyncHandler == null)
            {
                Action action = delegate {
                    handler.ProcessRequest(context.Unwrap());
                };
                return(AsyncResultWrapper.BeginSynchronous(callback, state, action, _tag));
            }
            BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState) {
                return(asyncHandler.BeginProcessRequest(context.Unwrap(), asyncCallback, asyncState));
            };
            EndInvokeDelegate endDelegate = delegate(IAsyncResult asyncResult) {
                asyncHandler.EndProcessRequest(asyncResult);
            };

            return(AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, _tag));
        }
Example #9
0
        public IAsyncResult BeginInvokeAction(ControllerContext context,
                                              string actionName, AsyncCallback callback, object state,
                                              IDictionary <string, object> values)
        {
            Precondition.Require(context, () => Error.ArgumentNull("context"));
            Precondition.Defined(actionName, () => Error.ArgumentNull("actionName"));

            ControllerDescriptor controller = GetControllerDescriptor(context);
            ActionDescriptor     action     = controller.FindAction(context, actionName);

            if (action == null)
            {
                return(ActionNotFound(callback, state));
            }

            context.Parameters.Merge(GetParameterValues(context, action))
            .Merge(values);

            ActionFilterInfo filters      = GetFilters(context, action);
            Action           continuation = null;

            BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState) {
                try
                {
                    AuthorizationContext authContext = InvokeAuthorizationFilters(context, action, filters.AuthorizationFilters);
                    if (authContext.Cancel)
                    {
                        continuation = () => InvokeActionResult(context, authContext.Result ?? EmptyResult.Instance);
                    }
                    else
                    {
                        if (context.Controller.ValidateRequest)
                        {
                            ValidateRequest(context.Context.Request);
                        }

                        IAsyncResult asyncResult = BeginInvokeActionFilters(context, action,
                                                                            filters.ActionFilters, context.Parameters, asyncCallback, asyncState);

                        continuation = () => {
                            ActionExecutedContext postContext = EndInvokeActionFilters(asyncResult);
                            InvokeActionResultFilters(context, filters.ResultFilters, postContext.Result);
                        };
                        return(asyncResult);
                    }
                }
                catch (ThreadAbortException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    ExceptionContext ctx = InvokeExceptionFilters(context, ex, filters.ExceptionFilters);
                    if (!ctx.Handled)
                    {
                        throw;
                    }

                    continuation = () => InvokeActionResult(context, ctx.Result);
                }
                return(MakeSynchronousAsyncResult(asyncCallback, asyncState));
            };

            EndInvokeDelegate <bool> endDelegate = delegate(IAsyncResult asyncResult) {
                try
                {
                    continuation();
                }
                catch (ThreadAbortException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    ExceptionContext ctx = InvokeExceptionFilters(context, ex, filters.ExceptionFilters);
                    if (!ctx.Handled)
                    {
                        throw;
                    }

                    InvokeActionResult(context, ctx.Result);
                }
                return(true);
            };

            return(AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, _invokeTag));
        }