public override IAsyncResult BeginExecute(ControllerContext controllerContext, IDictionary<string, object> parameters, AsyncCallback callback, object state) {
            if (controllerContext == null) {
                throw new ArgumentNullException("controllerContext");
            }
            if (parameters == null) {
                throw new ArgumentNullException("parameters");
            }

            AsyncManager asyncHelper = GetAsyncManager(controllerContext.Controller);
            SingleEntryGate setupCompletedGate = new SingleEntryGate();

            BeginInvokeCallback beginCallback = (innerCallback, innerState) => {
                ManualAsyncResult asyncResult = new ManualAsyncResult() {
                    AsyncState = innerState
                };

                // Get parameters for async setup method, then execute
                ParameterInfo[] setupParametersInfos = SetupMethod.GetParameters();
                var rawSetupParameterValues = from parameterInfo in setupParametersInfos
                                              select ExtractParameterFromDictionary(parameterInfo, parameters, SetupMethod);
                object[] setupParametersArray = rawSetupParameterValues.ToArray();

                // to simplify the logic, force an asynchronous callback
                asyncHelper.OutstandingOperations.Completed += delegate {
                    if (setupCompletedGate.TryEnter()) {
                        ThreadPool.QueueUserWorkItem(o => {
                            asyncResult.MarkCompleted(false /* completedSynchronously */, innerCallback);
                        });
                    }
                };

                MethodDispatcher setupDispatcher = DispatcherCache.GetDispatcher(SetupMethod);
                asyncHelper.OutstandingOperations.Increment();
                setupDispatcher.Execute(controllerContext.Controller, setupParametersArray);
                asyncHelper.OutstandingOperations.Decrement();
                return asyncResult;
            };

            AsyncCallback<object> endCallback = ar => {
                if (setupCompletedGate.TryEnter()) {
                    // the setup method did not complete before this callback executed
                    throw new InvalidOperationException(MvcResources.AsyncActionDescriptor_EndExecuteCalledPrematurely);
                }

                // Get parameters for action method, then execute
                ParameterInfo[] completionParametersInfos = CompletionMethod.GetParameters();
                var rawCompletionParameterValues = from parameterInfo in completionParametersInfos
                                                   select ExtractParameterOrDefaultFromDictionary(parameterInfo, asyncHelper.Parameters);
                object[] completionParametersArray = rawCompletionParameterValues.ToArray();

                MethodDispatcher completionDispatcher = DispatcherCache.GetDispatcher(CompletionMethod);
                object actionReturnValue = completionDispatcher.Execute(controllerContext.Controller, completionParametersArray);
                return actionReturnValue;
            };

            // Set the timeout and go
            int timeout = asyncHelper.Timeout;
            return AsyncResultWrapper.WrapWithTimeout(callback, state, beginCallback, endCallback, timeout, _executeTag);
        }
Ejemplo n.º 2
0
        public void CompletedSynchronouslyPropertySetToFalse() {
            // Arrange
            ManualAsyncResult asyncResult = new ManualAsyncResult();

            // Act
            asyncResult.MarkCompleted(false /* completedSynchronously */, null);

            // Assert
            Assert.IsFalse(asyncResult.CompletedSynchronously);
        }
Ejemplo n.º 3
0
        public void WaitHandlePropertyIsNotSetByDefault() {
            // Arrange
            ManualAsyncResult asyncResult = new ManualAsyncResult();

            // Act
            bool wasSet = asyncResult.AsyncWaitHandle.WaitOne(0, false /* exitContext */);

            // Assert
            Assert.IsFalse(wasSet);
        }
Ejemplo n.º 4
0
        public void IsCompletedProperty() {
            // Arrange
            ManualAsyncResult asyncResult = new ManualAsyncResult();

            // Act
            bool isCompletedBefore = asyncResult.IsCompleted;
            asyncResult.MarkCompleted(false, null);
            bool isCompletedAfter = asyncResult.IsCompleted;

            // Assert
            Assert.IsFalse(isCompletedBefore);
            Assert.IsTrue(isCompletedAfter);
        }
Ejemplo n.º 5
0
        private static IAsyncResult CreateInvokeActionNotFoundAsyncResult(AsyncCallback callback, object state)
        {
            BeginInvokeCallback beginCallback = (innerCallback, innerState) => {
                ManualAsyncResult asyncResult = new ManualAsyncResult()
                {
                    AsyncState = innerState
                };
                asyncResult.MarkCompleted(true /* completedSynchronously */, innerCallback);
                return(asyncResult);
            };
            AsyncCallback <bool> endCallback = ar => {
                return(false);
            };

            return(AsyncResultWrapper.Wrap(callback, state, beginCallback, endCallback, _invokeActionTag));
        }
Ejemplo n.º 6
0
        public void MarkCompleted() {
            // Arrange
            ManualAsyncResult asyncResult = new ManualAsyncResult();
            bool callbackWasCalled = false;

            AsyncCallback callback = ar => {
                callbackWasCalled = true;
                Assert.AreEqual(asyncResult, ar);
                Assert.IsTrue(ar.CompletedSynchronously);
                Assert.IsTrue(ar.IsCompleted);

                bool wasSignaledBefore = ar.AsyncWaitHandle.WaitOne(0, false /* exitContext */);
                Assert.IsFalse(wasSignaledBefore, "The WaitHandle should not yet have been signaled.");
            };

            // Act
            asyncResult.MarkCompleted(true, callback);
            bool wasSignaledAfter = asyncResult.AsyncWaitHandle.WaitOne(0, false /* exitContext */);

            // Assert
            Assert.IsTrue(callbackWasCalled);
            Assert.IsTrue(wasSignaledAfter);
        }
        private static IAsyncResult CreateInvokeActionNotFoundAsyncResult(AsyncCallback callback, object state) {
            BeginInvokeCallback beginCallback = (innerCallback, innerState) => {
                ManualAsyncResult asyncResult = new ManualAsyncResult() { AsyncState = innerState };
                asyncResult.MarkCompleted(true /* completedSynchronously */, innerCallback);
                return asyncResult;
            };
            AsyncCallback<bool> endCallback = ar => {
                return false;
            };

            return AsyncResultWrapper.Wrap(callback, state, beginCallback, endCallback, _invokeActionTag);
        }
        public override IAsyncResult BeginExecute(ControllerContext controllerContext, IDictionary<string, object> parameters, AsyncCallback callback, object state) {
            if (controllerContext == null) {
                throw new ArgumentNullException("controllerContext");
            }
            if (parameters == null) {
                throw new ArgumentNullException("parameters");
            }

            AsyncManager asyncHelper = GetAsyncManager(controllerContext.Controller);
            SingleFireEvent setupCompletedEvent = new SingleFireEvent();
            ContinuationListener listener = new ContinuationListener();
            object theDelegate = null;

            BeginInvokeCallback beginCallback = (innerCallback, innerState) => {
                ManualAsyncResult asyncResult = new ManualAsyncResult() {
                    AsyncState = innerState
                };

                // Get parameters for async setup method, then execute
                ParameterInfo[] setupParametersInfos = ActionMethod.GetParameters();
                var rawSetupParameterValues = from parameterInfo in setupParametersInfos
                                              select ExtractParameterFromDictionary(parameterInfo, parameters, ActionMethod);
                object[] setupParametersArray = rawSetupParameterValues.ToArray();

                // to simplify the logic, force an asynchronous callback
                asyncHelper.OutstandingOperations.Completed += delegate {
                    if (setupCompletedEvent.Signal()) {
                        listener.SetContinuation(() => {
                            ThreadPool.QueueUserWorkItem(o => {
                                asyncResult.MarkCompleted(false /* completedSynchronously */, innerCallback);
                            });
                        });
                    }
                };

                MethodDispatcher setupDispatcher = DispatcherCache.GetDispatcher(ActionMethod);
                asyncHelper.OutstandingOperations.Increment();
                object returnedDelegate = setupDispatcher.Execute(controllerContext.Controller, setupParametersArray);
                ValidateDelegateNotNull(returnedDelegate, ActionMethod);
                asyncHelper.OutstandingOperations.Decrement();

                Thread.VolatileWrite(ref theDelegate, returnedDelegate);
                listener.Signal();
                return asyncResult;
            };

            AsyncCallback<object> endCallback = ar => {
                if (setupCompletedEvent.Signal()) {
                    // the setup method did not complete before this callback executed
                    throw new InvalidOperationException(MvcResources.AsyncActionDescriptor_EndExecuteCalledPrematurely);
                }

                object returnedDelegate = Thread.VolatileRead(ref theDelegate);
                MethodInfo invokeMethod = returnedDelegate.GetType().GetMethod("Invoke", Type.EmptyTypes);

                MethodDispatcher invokeDispatcher = DispatcherCache.GetDispatcher(invokeMethod);
                object actionReturnValue = invokeDispatcher.Execute(returnedDelegate, _emptyParameters);
                return actionReturnValue;
            };

            // Set the timeout and go
            int timeout = asyncHelper.Timeout;
            return AsyncResultWrapper.WrapWithTimeout(callback, state, beginCallback, endCallback, timeout, _executeTag);
        }
Ejemplo n.º 9
0
        public void WaitHandlePropertyIsActivelySetOnCompletion() {
            // Arrange
            ManualAsyncResult asyncResult = new ManualAsyncResult();

            // Act
            bool wasSet1 = asyncResult.AsyncWaitHandle.WaitOne(0, false /* exitContext */);
            asyncResult.MarkCompleted(false, null);
            bool wasSet2 = asyncResult.AsyncWaitHandle.WaitOne(0, false /* exitContext */);

            // Assert
            Assert.IsFalse(wasSet1);
            Assert.IsTrue(wasSet2);
        }
        public override IAsyncResult BeginExecute(ControllerContext controllerContext, IDictionary <string, object> parameters, AsyncCallback callback, object state)
        {
            if (controllerContext == null)
            {
                throw new ArgumentNullException("controllerContext");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            AsyncManager    asyncHelper         = GetAsyncManager(controllerContext.Controller);
            SingleFireEvent setupCompletedEvent = new SingleFireEvent();

            BeginInvokeCallback beginCallback = (innerCallback, innerState) => {
                ManualAsyncResult asyncResult = new ManualAsyncResult()
                {
                    AsyncState = innerState
                };

                // Get parameters for async setup method, then execute
                ParameterInfo[] setupParametersInfos    = SetupMethod.GetParameters();
                var             rawSetupParameterValues = from parameterInfo in setupParametersInfos
                                                          select ExtractParameterFromDictionary(parameterInfo, parameters, SetupMethod);

                object[] setupParametersArray = rawSetupParameterValues.ToArray();

                // to simplify the logic, force an asynchronous callback
                asyncHelper.OutstandingOperations.Completed += delegate {
                    if (setupCompletedEvent.Signal())
                    {
                        ThreadPool.QueueUserWorkItem(o => {
                            asyncResult.MarkCompleted(false /* completedSynchronously */, innerCallback);
                        });
                    }
                };

                MethodDispatcher setupDispatcher = DispatcherCache.GetDispatcher(SetupMethod);
                asyncHelper.OutstandingOperations.Increment();
                setupDispatcher.Execute(controllerContext.Controller, setupParametersArray);
                asyncHelper.OutstandingOperations.Decrement();
                return(asyncResult);
            };

            AsyncCallback <object> endCallback = ar => {
                if (setupCompletedEvent.Signal())
                {
                    // the setup method did not complete before this callback executed
                    throw new InvalidOperationException(MvcResources.AsyncActionDescriptor_EndExecuteCalledPrematurely);
                }

                // Get parameters for action method, then execute
                ParameterInfo[] completionParametersInfos    = CompletionMethod.GetParameters();
                var             rawCompletionParameterValues = from parameterInfo in completionParametersInfos
                                                               select ExtractParameterOrDefaultFromDictionary(parameterInfo, asyncHelper.Parameters);

                object[] completionParametersArray = rawCompletionParameterValues.ToArray();

                MethodDispatcher completionDispatcher = DispatcherCache.GetDispatcher(CompletionMethod);
                object           actionReturnValue    = completionDispatcher.Execute(controllerContext.Controller, completionParametersArray);
                return(actionReturnValue);
            };

            // Set the timeout and go
            int timeout = asyncHelper.Timeout;

            return(AsyncResultWrapper.WrapWithTimeout(callback, state, beginCallback, endCallback, timeout, _executeTag));
        }