protected internal virtual IAsyncResult BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, object state)
        {
            return(SecurityUtil.ProcessInApplicationTrust(() =>
            {
                IController controller;
                IControllerFactory factory;
                ProcessRequestInit(httpContext, out controller, out factory);

                IAsyncController asyncController = controller as IAsyncController;
                if (asyncController != null)
                {
                    // asynchronous controller
                    BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState)
                    {
                        try
                        {
                            return asyncController.BeginExecute(RequestContext, asyncCallback, asyncState);
                        }
                        catch
                        {
                            factory.ReleaseController(asyncController);
                            throw;
                        }
                    };

                    EndInvokeDelegate endDelegate = delegate(IAsyncResult asyncResult)
                    {
                        try
                        {
                            asyncController.EndExecute(asyncResult);
                        }
                        finally
                        {
                            factory.ReleaseController(asyncController);
                        }
                    };

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

                    return AsyncResultWrapper.BeginSynchronous(callback, state, action, _processRequestTag);
                }
            }));
        }
        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);
                return(AsyncResultWrapper.Begin(callback, state, BeginExecuteCore, EndExecuteCore, _executeTag));
            }
        }
Beispiel #3
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
                BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState)
                {
                    return(httpAsyncHandler.BeginProcessRequest(HttpContext.Current, asyncCallback, asyncState));
                };
                EndInvokeDelegate endDelegate = delegate(IAsyncResult asyncResult)
                {
                    httpAsyncHandler.EndProcessRequest(asyncResult);
                };
                return(AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, _processRequestTag));
            }
            else
            {
                // synchronous handler
                Action action = delegate
                {
                    httpHandler.ProcessRequest(HttpContext.Current);
                };
                return(AsyncResultWrapper.BeginSynchronous(callback, state, action, _processRequestTag));
            }
        }
Beispiel #4
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
                           ));
            }
        }
Beispiel #5
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
                           ));
            }
        }
        public void BeginSynchronous_Func()
        {
            // Act
            IAsyncResult asyncResult = AsyncResultWrapper.BeginSynchronous(null, null, () => 42);
            int          retVal      = AsyncResultWrapper.End <int>(asyncResult);

            // Assert
            Assert.Equal(42, retVal);
            Assert.True(asyncResult.IsCompleted);
            Assert.True(asyncResult.CompletedSynchronously);
        }
Beispiel #7
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;
            }
        }
        public void BeginSynchronous_Action()
        {
            // Arrange
            bool actionCalled = false;

            // Act
            IAsyncResult asyncResult = AsyncResultWrapper.BeginSynchronous(null, null, delegate { actionCalled = true; });

            AsyncResultWrapper.End(asyncResult);

            // Assert
            Assert.True(actionCalled);
            Assert.True(asyncResult.IsCompleted);
            Assert.True(asyncResult.CompletedSynchronously);
        }
        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
                    BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState)
                    {
                        return(asyncInvoker.BeginInvokeAction(ControllerContext, actionName, asyncCallback, asyncState));
                    };

                    EndInvokeDelegate endDelegate = delegate(IAsyncResult asyncResult)
                    {
                        if (!asyncInvoker.EndInvokeAction(asyncResult))
                        {
                            HandleUnknownAction(actionName);
                        }
                    };

                    return(AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, _executeCoreTag));
                }
                else
                {
                    // synchronous invocation
                    Action action = () =>
                    {
                        if (!invoker.InvokeAction(ControllerContext, actionName))
                        {
                            HandleUnknownAction(actionName);
                        }
                    };
                    return(AsyncResultWrapper.BeginSynchronous(callback, state, action, _executeCoreTag));
                }
            }
            catch
            {
                PossiblySaveTempData();
                throw;
            }
        }
Beispiel #10
0
        public void BeginSynchronous_Func()
        {
            object expectedReturn        = new object();
            object expectedState         = new object();
            object expectedCallbackState = new object();
            bool   funcCalled            = false;
            bool   asyncCalledbackCalled = false;

            // Act
            IAsyncResult asyncResult = AsyncResultWrapper.BeginSynchronous(
                callback: (innerIAsyncResult) =>
            {
                asyncCalledbackCalled = true;
                Assert.NotNull(innerIAsyncResult);
                Assert.Same(expectedCallbackState, innerIAsyncResult.AsyncState);
                Assert.True(innerIAsyncResult.IsCompleted);
                Assert.True(innerIAsyncResult.CompletedSynchronously);
            },
                callbackState: expectedCallbackState,
                func: (innerIAsyncResult, innerState) =>
            {
                funcCalled = true;
                Assert.NotNull(innerIAsyncResult);
                Assert.Same(expectedCallbackState, innerIAsyncResult.AsyncState);
                Assert.Same(expectedState, innerState);
                Assert.True(innerIAsyncResult.IsCompleted);
                Assert.True(innerIAsyncResult.CompletedSynchronously);
                return(expectedReturn);
            },
                funcState: expectedState,
                tag: null
                );
            object retVal = AsyncResultWrapper.End <object>(asyncResult);

            // Assert
            Assert.Same(expectedReturn, retVal);
            Assert.True(asyncResult.IsCompleted);
            Assert.True(asyncResult.CompletedSynchronously);
            Assert.True(funcCalled);
            Assert.True(asyncCalledbackCalled);
        }
Beispiel #11
0
        protected internal virtual IAsyncResult BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, object state)
        {
            BeginInvokeDelegate delegate4      = null;
            EndInvokeDelegate   delegate5      = null;
            Action            action2          = null;
            IHttpHandler      httpHandler      = GetHttpHandler(httpContext);
            IHttpAsyncHandler httpAsyncHandler = httpHandler as IHttpAsyncHandler;

            if (httpAsyncHandler != null)
            {
                if (delegate4 == null)
                {
                    delegate4 = (asyncCallback, asyncState) => httpAsyncHandler.BeginProcessRequest(HttpContext.Current, asyncCallback, asyncState);
                }
                BeginInvokeDelegate beginDelegate = delegate4;
                if (delegate5 == null)
                {
                    delegate5 = delegate(IAsyncResult asyncResult)
                    {
                        httpAsyncHandler.EndProcessRequest(asyncResult);
                    };
                }
                EndInvokeDelegate endDelegate = delegate5;
                return(AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, _processRequestTag));
            }
            if (action2 == null)
            {
                action2 = delegate
                {
                    httpHandler.ProcessRequest(HttpContext.Current);
                }
            }
            ;
            Action action = action2;

            return(AsyncResultWrapper.BeginSynchronous(callback, state, action, _processRequestTag));
        }
Beispiel #12
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
                           ));
            }
        }