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);
                }
            }));
        }
Esempio n. 2
0
        public void ExecuteCallsInitialize()
        {
            // Arrange
            RequestContext  requestContext = new RequestContext(new Mock <HttpContextBase>().Object, new RouteData());
            MockAsyncResult asyncResult    = new MockAsyncResult();

            Mock <AsyncController> mockController = new Mock <AsyncController>()
            {
                CallBase = true
            };

            mockController.Expect(c => c.BeginExecuteCore(It.IsAny <AsyncCallback>(), It.IsAny <object>())).Returns(asyncResult).Verifiable();
            mockController.Expect(c => c.EndExecuteCore(asyncResult)).Verifiable();

            AsyncController  controller  = mockController.Object;
            IAsyncController iController = controller;

            // Act
            IAsyncResult returnedAsyncResult = iController.BeginExecute(requestContext, null, null);

            iController.EndExecute(returnedAsyncResult);

            // Assert
            Assert.AreEqual(requestContext, controller.ControllerContext.RequestContext);
            mockController.Verify();
        }
        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));
        }