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);
                }
            }));
        }
        public void SyncWithAction()
        {
            // Arrange
            bool actionWasCalled = false;
            bool sendWasCalled   = false;

            Mock <SynchronizationContext> mockSyncContext = new Mock <SynchronizationContext>();

            mockSyncContext
            .Setup(sc => sc.Send(It.IsAny <SendOrPostCallback>(), null))
            .Callback(
                delegate(SendOrPostCallback d, object state)
            {
                sendWasCalled = true;
                d(state);
            }
                );

            // Act
            SynchronizationContextUtil.Sync(
                mockSyncContext.Object,
                () =>
            {
                actionWasCalled = true;
            }
                );

            // Assert
            Assert.True(actionWasCalled);
            Assert.True(sendWasCalled);
        }
        public void SyncWithActionCapturesException()
        {
            // Arrange
            InvalidOperationException exception = new InvalidOperationException("Some exception text.");

            Mock <SynchronizationContext> mockSyncContext = new Mock <SynchronizationContext>();

            mockSyncContext
            .Setup(sc => sc.Send(It.IsAny <SendOrPostCallback>(), null))
            .Callback(
                delegate(SendOrPostCallback d, object state)
            {
                try
                {
                    d(state);
                }
                catch
                {
                    // swallow exceptions, just like AspNetSynchronizationContext
                }
            });

            // Act & assert
            SynchronousOperationException thrownException = Assert.Throws <SynchronousOperationException>(
                delegate { SynchronizationContextUtil.Sync(mockSyncContext.Object, () => { throw exception; }); },
                @"An operation that crossed a synchronization context failed. See the inner exception for more information.");

            Assert.Equal(exception, thrownException.InnerException);
        }
Beispiel #4
0
            internal SyncEventMethodInfo(MethodInfo methodInfo, bool isArgless)
            {
                if (IsAsyncVoidMethod(methodInfo))
                {
                    SynchronizationContextUtil.ValidateModeForPageAsyncVoidMethods();
                }

                MethodInfo = methodInfo;
                IsArgless  = isArgless;
            }
Beispiel #5
0
        // TAP
        internal PageAsyncTask(Func <CancellationToken, Task> handler, SynchronizationContextMode currentMode)
        {
            if (handler == null)
            {
                throw new ArgumentNullException("handler");
            }

            // The legacy PageAsyncTaskManager doesn't support TAP methods
            SynchronizationContextUtil.ValidateMode(currentMode, requiredMode: SynchronizationContextMode.Normal, specificErrorMessage: SR.SynchronizationContextUtil_TaskReturningPageAsyncMethodsNotCompatible);

            TaskHandler = handler;
        }
        public void SyncWithFunc()
        {
            // Arrange
            bool sendWasCalled = false;

            Mock <SynchronizationContext> mockSyncContext = new Mock <SynchronizationContext>();

            mockSyncContext
            .Expect(sc => sc.Send(It.IsAny <SendOrPostCallback>(), null))
            .Callback(
                delegate(SendOrPostCallback d, object state) {
                sendWasCalled = true;
                d(state);
            });

            // Act
            int retVal = SynchronizationContextUtil.Sync(mockSyncContext.Object, () => 42);

            // Assert
            Assert.AreEqual(42, retVal);
            Assert.IsTrue(sendWasCalled);
        }
Beispiel #7
0
        // APM
        internal PageAsyncTask(BeginEventHandler beginHandler, EndEventHandler endHandler, EndEventHandler timeoutHandler, Object state, bool executeInParallel, SynchronizationContextMode currentMode)
        {
            if (beginHandler == null)
            {
                throw new ArgumentNullException("beginHandler");
            }
            if (endHandler == null)
            {
                throw new ArgumentNullException("endHandler");
            }

            // Only the legacy PageAsyncTaskManager supports timing out APM methods or executing them in parallel
            if (timeoutHandler != null || executeInParallel)
            {
                SynchronizationContextUtil.ValidateMode(currentMode, requiredMode: SynchronizationContextMode.Legacy, specificErrorMessage: SR.SynchronizationContextUtil_PageAsyncTaskTimeoutHandlerParallelNotCompatible);
            }

            BeginHandler      = beginHandler;
            EndHandler        = endHandler;
            TimeoutHandler    = timeoutHandler;
            State             = state;
            ExecuteInParallel = executeInParallel;
        }
Beispiel #8
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
                           ));
            }
        }