public void ExecuteWithStaticMethod() { // Arrange MethodContainer container = new MethodContainer(); object[] parameters = new object[0]; MethodInfo methodInfo = typeof(MethodContainer).GetMethod("StaticMethod"); MethodDispatcher dispatcher = new MethodDispatcher(methodInfo); // Act object returnValue = dispatcher.Execute(container, parameters); // Assert Assert.AreEqual(89, returnValue); }
public void ExecuteWithVoidInstanceMethod() { // Arrange MethodContainer container = new MethodContainer(); object[] parameters = new object[] { 5, "some string", new DateTime(2001, 1, 1) }; MethodInfo methodInfo = typeof(MethodContainer).GetMethod("VoidMethod"); MethodDispatcher dispatcher = new MethodDispatcher(methodInfo); // Act object returnValue = dispatcher.Execute(container, parameters); // Assert Assert.IsNull(returnValue); Assert.AreEqual(5, container._i); Assert.AreEqual("some string", container._s); Assert.AreEqual(new DateTime(2001, 1, 1), container._dt); }
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"); } ParameterInfo[] parameterInfos = BeginMethod.GetParameters(); IEnumerable <object> rawParameterValues = from parameterInfo in parameterInfos select ExtractParameterFromDictionary(parameterInfo, parameters, BeginMethod); List <object> parametersList = rawParameterValues.Take(parameterInfos.Length - 2).ToList(); MethodDispatcher beginDispatcher = DispatcherCache.GetDispatcher(BeginMethod); MethodDispatcher endDispatcher = DispatcherCache.GetDispatcher(EndMethod); // need to add callback + state object to list BeginInvokeCallback beginCallback = (innerCallback, innerState) => { parametersList.Add(innerCallback); parametersList.Add(innerState); object[] parametersArray = parametersList.ToArray(); IAsyncResult innerAsyncResult = (IAsyncResult)beginDispatcher.Execute(controllerContext.Controller, parametersArray); return(innerAsyncResult); }; AsyncCallback <object> endCallback = ar => { object actionReturnValue = endDispatcher.Execute(controllerContext.Controller, new object[] { ar }); return(actionReturnValue); }; // Set the timeout and go IAsyncManagerContainer helperContainer = controllerContext.Controller as IAsyncManagerContainer; int timeout = (helperContainer != null) ? helperContainer.AsyncManager.Timeout : Timeout.Infinite; return(AsyncResultWrapper.WrapWithTimeout(callback, state, beginCallback, endCallback, timeout, _executeTag)); }
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)); }