Example #1
0
        protected internal virtual IAsyncResult BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, object state)
        {
            IHttpHandler      httpHandler      = GetHttpHandler(httpContext);
            IHttpAsyncHandler httpAsyncHandler = httpHandler as IHttpAsyncHandler;

            if (httpAsyncHandler != null)
            {
                // asynchronous handler

                // Ensure delegates continue to use the C# Compiler static delegate caching optimization.
                BeginInvokeDelegate <IHttpAsyncHandler> beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState, IHttpAsyncHandler innerHandler)
                {
                    return(innerHandler.BeginProcessRequest(HttpContext.Current, asyncCallback, asyncState));
                };
                EndInvokeVoidDelegate <IHttpAsyncHandler> endDelegate = delegate(IAsyncResult asyncResult, IHttpAsyncHandler innerHandler)
                {
                    innerHandler.EndProcessRequest(asyncResult);
                };
                return(AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, httpAsyncHandler, _processRequestTag));
            }
            else
            {
                // synchronous handler
                Action action = delegate
                {
                    httpHandler.ProcessRequest(HttpContext.Current);
                };
                return(AsyncResultWrapper.BeginSynchronous(callback, state, action, _processRequestTag));
            }
        }
 public WrappedAsyncVoid(BeginInvokeDelegate<TState> beginDelegate, EndInvokeVoidDelegate<TState> endDelegate, TState state, object tag, SynchronizationContext callbackSyncContext)
     : base(tag, callbackSyncContext)
 {
     _beginDelegate = beginDelegate;
     _endDelegate = endDelegate;
     _state = state;
 }
Example #3
0
        protected virtual IAsyncResult BeginExecute(
            RequestContext requestContext,
            AsyncCallback callback,
            object state
            )
        {
            if (DisableAsyncSupport)
            {
                // For backwards compat, we can disallow async support and just chain to the sync Execute() function.
                Action action = () =>
                {
                    Execute(requestContext);
                };

                return(AsyncResultWrapper.BeginSynchronous(callback, state, action, _executeTag));
            }
            else
            {
                if (requestContext == null)
                {
                    throw new ArgumentNullException("requestContext");
                }

                // Support Asynchronous behavior.
                // Execute/ExecuteCore are no longer called.

                VerifyExecuteCalledOnce();
                Initialize(requestContext);

                // Ensure delegates continue to use the C# Compiler static delegate caching optimization.
                BeginInvokeDelegate <Controller> beginDelegate = (
                    AsyncCallback asyncCallback,
                    object callbackState,
                    Controller controller
                    ) =>
                {
                    return(controller.BeginExecuteCore(asyncCallback, callbackState));
                };
                EndInvokeVoidDelegate <Controller> endDelegate = (
                    IAsyncResult asyncResult,
                    Controller controller
                    ) =>
                {
                    controller.EndExecuteCore(asyncResult);
                };
                return(AsyncResultWrapper.Begin(
                           callback,
                           state,
                           beginDelegate,
                           endDelegate,
                           this,
                           _executeTag
                           ));
            }
        }
Example #4
0
        protected virtual IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
        {
            // If code in this method needs to be updated, please also check the ExecuteCore() method
            // of Controller to see if that code also must be updated.
            PossiblyLoadTempData();
            try
            {
                string              actionName   = RouteData.GetRequiredString("action");
                IActionInvoker      invoker      = ActionInvoker;
                IAsyncActionInvoker asyncInvoker = invoker as IAsyncActionInvoker;
                if (asyncInvoker != null)
                {
                    // asynchronous invocation
                    // Ensure delegates continue to use the C# Compiler static delegate caching optimization.
                    BeginInvokeDelegate <ExecuteCoreState> beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState, ExecuteCoreState innerState)
                    {
                        return(innerState.AsyncInvoker.BeginInvokeAction(innerState.Controller.ControllerContext, innerState.ActionName, asyncCallback, asyncState));
                    };

                    EndInvokeVoidDelegate <ExecuteCoreState> endDelegate = delegate(IAsyncResult asyncResult, ExecuteCoreState innerState)
                    {
                        if (!innerState.AsyncInvoker.EndInvokeAction(asyncResult))
                        {
                            innerState.Controller.HandleUnknownAction(innerState.ActionName);
                        }
                    };
                    ExecuteCoreState executeState = new ExecuteCoreState()
                    {
                        Controller = this, AsyncInvoker = asyncInvoker, ActionName = actionName
                    };

                    return(AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, executeState, _executeCoreTag));
                }
                else
                {
                    // synchronous invocation
                    Action action = () =>
                    {
                        if (!invoker.InvokeAction(ControllerContext, actionName))
                        {
                            HandleUnknownAction(actionName);
                        }
                    };
                    return(AsyncResultWrapper.BeginSynchronous(callback, state, action, _executeCoreTag));
                }
            }
            catch
            {
                PossiblySaveTempData();
                throw;
            }
        }
Example #5
0
        public static IAsyncResult Begin <TState>(
            AsyncCallback callback,
            object callbackState,
            BeginInvokeDelegate <TState> beginDelegate,
            EndInvokeVoidDelegate <TState> endDelegate,
            TState invokeState,
            object tag  = null,
            int timeout = Timeout.Infinite,
            SynchronizationContext callbackSyncContext = null
            )
        {
            WrappedAsyncVoid <TState> asyncResult = new WrappedAsyncVoid <TState>(
                beginDelegate,
                endDelegate,
                invokeState,
                tag,
                callbackSyncContext
                );

            asyncResult.Begin(callback, callbackState, timeout);
            return(asyncResult);
        }
Example #6
0
        protected internal virtual IAsyncResult BeginProcessRequest(
            HttpContextBase httpContext,
            AsyncCallback callback,
            object state
            )
        {
            IController        controller;
            IControllerFactory factory;

            ProcessRequestInit(httpContext, out controller, out factory);

            IAsyncController asyncController = controller as IAsyncController;

            if (asyncController != null)
            {
                // asynchronous controller

                // Ensure delegates continue to use the C# Compiler static delegate caching optimization.
                BeginInvokeDelegate <ProcessRequestState> beginDelegate = delegate(
                    AsyncCallback asyncCallback,
                    object asyncState,
                    ProcessRequestState innerState
                    )
                {
                    try
                    {
                        return(innerState.AsyncController.BeginExecute(
                                   innerState.RequestContext,
                                   asyncCallback,
                                   asyncState
                                   ));
                    }
                    catch
                    {
                        innerState.ReleaseController();
                        throw;
                    }
                };

                EndInvokeVoidDelegate <ProcessRequestState> endDelegate = delegate(
                    IAsyncResult asyncResult,
                    ProcessRequestState innerState
                    )
                {
                    try
                    {
                        innerState.AsyncController.EndExecute(asyncResult);
                    }
                    finally
                    {
                        innerState.ReleaseController();
                    }
                };
                ProcessRequestState outerState = new ProcessRequestState()
                {
                    AsyncController = asyncController,
                    Factory         = factory,
                    RequestContext  = RequestContext
                };

                SynchronizationContext callbackSyncContext =
                    SynchronizationContextUtil.GetSynchronizationContext();
                return(AsyncResultWrapper.Begin(
                           callback,
                           state,
                           beginDelegate,
                           endDelegate,
                           outerState,
                           _processRequestTag,
                           callbackSyncContext: callbackSyncContext
                           ));
            }
            else
            {
                // synchronous controller
                Action action = delegate
                {
                    try
                    {
                        controller.Execute(RequestContext);
                    }
                    finally
                    {
                        factory.ReleaseController(controller);
                    }
                };

                return(AsyncResultWrapper.BeginSynchronous(
                           callback,
                           state,
                           action,
                           _processRequestTag
                           ));
            }
        }