private static IAsyncResult BeginInvokeAction_MakeSynchronousAsyncResult( AsyncCallback callback, object state ) { SimpleAsyncResult asyncResult = new SimpleAsyncResult(state); asyncResult.MarkCompleted( true /* completedSynchronously */ , callback ); return(asyncResult); }
protected internal virtual IAsyncResult BeginInvokeActionMethodWithFilters(ControllerContext controllerContext, IList <IActionFilter> filters, ActionDescriptor actionDescriptor, IDictionary <string, object> parameters, AsyncCallback callback, object state) { Func <ActionExecutedContext> endContinuation = null; BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState) { ActionExecutingContext preContext = new ActionExecutingContext(controllerContext, actionDescriptor, parameters); IAsyncResult innerAsyncResult = null; Func <Func <ActionExecutedContext> > beginContinuation = () => { innerAsyncResult = BeginInvokeActionMethod(controllerContext, actionDescriptor, parameters, asyncCallback, asyncState); return(() => new ActionExecutedContext(controllerContext, actionDescriptor, false /* canceled */, null /* exception */) { Result = EndInvokeActionMethod(innerAsyncResult) }); }; // need to reverse the filter list because the continuations are built up backward Func <Func <ActionExecutedContext> > thunk = filters.Reverse().Aggregate(beginContinuation, (next, filter) => () => InvokeActionMethodFilterAsynchronously(filter, preContext, next)); endContinuation = thunk(); if (innerAsyncResult != null) { // we're just waiting for the inner result to complete return(innerAsyncResult); } else { // something was short-circuited and the action was not called, so this was a synchronous operation SimpleAsyncResult newAsyncResult = new SimpleAsyncResult(asyncState); newAsyncResult.MarkCompleted(true /* completedSynchronously */, asyncCallback); return(newAsyncResult); } }; EndInvokeDelegate <ActionExecutedContext> endDelegate = delegate(IAsyncResult asyncResult) { return(endContinuation()); }; return(AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, _invokeActionMethodWithFiltersTag)); }
public static IAsyncResult BeginSynchronous <TResult>(AsyncCallback callback, object state, Func <TResult> func, object tag) { // Begin() doesn't perform any work on its own and returns immediately. BeginInvokeDelegate beginDelegate = (asyncCallback, asyncState) => { SimpleAsyncResult innerAsyncResult = new SimpleAsyncResult(asyncState); innerAsyncResult.MarkCompleted(true /* completedSynchronously */, asyncCallback); return(innerAsyncResult); }; // The End() method blocks. EndInvokeDelegate <TResult> endDelegate = _ => { return(func()); }; WrappedAsyncResult <TResult> asyncResult = new WrappedAsyncResult <TResult>(beginDelegate, endDelegate, tag); asyncResult.Begin(callback, state, Timeout.Infinite); return(asyncResult); }
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 asyncManager = GetAsyncManager(controllerContext.Controller); BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState) { // call the XxxAsync() method ParameterInfo[] parameterInfos = AsyncMethodInfo.GetParameters(); var rawParameterValues = from parameterInfo in parameterInfos select ExtractParameterFromDictionary(parameterInfo, parameters, AsyncMethodInfo); object[] parametersArray = rawParameterValues.ToArray(); TriggerListener listener = new TriggerListener(); SimpleAsyncResult asyncResult = new SimpleAsyncResult(asyncState); // hook the Finished event to notify us upon completion Trigger finishTrigger = listener.CreateTrigger(); asyncManager.Finished += delegate { finishTrigger.Fire(); }; asyncManager.OutstandingOperations.Increment(); // to simplify the logic, force the rest of the pipeline to execute in an asynchronous callback listener.SetContinuation(() => ThreadPool.QueueUserWorkItem(_ => asyncResult.MarkCompleted(false /* completedSynchronously */, asyncCallback))); // the inner operation might complete synchronously, so all setup work has to be done before this point ActionMethodDispatcher dispatcher = DispatcherCache.GetDispatcher(AsyncMethodInfo); dispatcher.Execute(controllerContext.Controller, parametersArray); // ignore return value from this method // now that the XxxAsync() method has completed, kick off any pending operations asyncManager.OutstandingOperations.Decrement(); listener.Activate(); return asyncResult; }; EndInvokeDelegate<object> endDelegate = delegate(IAsyncResult asyncResult) { // call the XxxCompleted() method ParameterInfo[] completionParametersInfos = CompletedMethodInfo.GetParameters(); var rawCompletionParameterValues = from parameterInfo in completionParametersInfos select ExtractParameterOrDefaultFromDictionary(parameterInfo, asyncManager.Parameters); object[] completionParametersArray = rawCompletionParameterValues.ToArray(); ActionMethodDispatcher dispatcher = DispatcherCache.GetDispatcher(CompletedMethodInfo); object actionReturnValue = dispatcher.Execute(controllerContext.Controller, completionParametersArray); return actionReturnValue; }; return AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, _executeTag, asyncManager.Timeout); }
protected internal virtual IAsyncResult BeginInvokeActionMethodWithFilters(ControllerContext controllerContext, IList<IActionFilter> filters, ActionDescriptor actionDescriptor, IDictionary<string, object> parameters, AsyncCallback callback, object state) { Func<ActionExecutedContext> endContinuation = null; BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState) { AsyncInvocationWithFilters invocation = new AsyncInvocationWithFilters(this, controllerContext, actionDescriptor, filters, parameters, asyncCallback, asyncState); const int StartingFilterIndex = 0; endContinuation = invocation.InvokeActionMethodFilterAsynchronouslyRecursive(StartingFilterIndex); if (invocation.InnerAsyncResult != null) { // we're just waiting for the inner result to complete return invocation.InnerAsyncResult; } else { // something was short-circuited and the action was not called, so this was a synchronous operation SimpleAsyncResult newAsyncResult = new SimpleAsyncResult(asyncState); newAsyncResult.MarkCompleted(completedSynchronously: true, callback: asyncCallback); return newAsyncResult; } }; EndInvokeDelegate<ActionExecutedContext> endDelegate = delegate(IAsyncResult asyncResult) { return endContinuation(); }; return AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, _invokeActionMethodWithFiltersTag); }
private static IAsyncResult BeginInvokeAction_MakeSynchronousAsyncResult(AsyncCallback callback, object state) { SimpleAsyncResult asyncResult = new SimpleAsyncResult(state); asyncResult.MarkCompleted(true /* completedSynchronously */, callback); return asyncResult; }
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 asyncManager = GetAsyncManager(controllerContext.Controller); BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState) { // call the XxxAsync() method ParameterInfo[] parameterInfos = AsyncMethodInfo.GetParameters(); var rawParameterValues = from parameterInfo in parameterInfos select ExtractParameterFromDictionary(parameterInfo, parameters, AsyncMethodInfo); object[] parametersArray = rawParameterValues.ToArray(); TriggerListener listener = new TriggerListener(); SimpleAsyncResult asyncResult = new SimpleAsyncResult(asyncState); // hook the Finished event to notify us upon completion Trigger finishTrigger = listener.CreateTrigger(); asyncManager.Finished += delegate { finishTrigger.Fire(); }; asyncManager.OutstandingOperations.Increment(); // to simplify the logic, force the rest of the pipeline to execute in an asynchronous callback listener.SetContinuation(() => ThreadPool.QueueUserWorkItem(_ => asyncResult.MarkCompleted(false /* completedSynchronously */, asyncCallback))); // the inner operation might complete synchronously, so all setup work has to be done before this point ActionMethodDispatcher dispatcher = DispatcherCache.GetDispatcher(AsyncMethodInfo); dispatcher.Execute(controllerContext.Controller, parametersArray); // ignore return value from this method // now that the XxxAsync() method has completed, kick off any pending operations asyncManager.OutstandingOperations.Decrement(); listener.Activate(); return(asyncResult); }; EndInvokeDelegate <object> endDelegate = delegate(IAsyncResult asyncResult) { // call the XxxCompleted() method ParameterInfo[] completionParametersInfos = CompletedMethodInfo.GetParameters(); var rawCompletionParameterValues = from parameterInfo in completionParametersInfos select ExtractParameterOrDefaultFromDictionary(parameterInfo, asyncManager.Parameters); object[] completionParametersArray = rawCompletionParameterValues.ToArray(); ActionMethodDispatcher dispatcher = DispatcherCache.GetDispatcher(CompletedMethodInfo); object actionReturnValue = dispatcher.Execute(controllerContext.Controller, completionParametersArray); return(actionReturnValue); }; return(AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, _executeTag, asyncManager.Timeout)); }
protected internal virtual IAsyncResult BeginInvokeActionMethodWithFilters(ControllerContext controllerContext, IList<IActionFilter> filters, ActionDescriptor actionDescriptor, IDictionary<string, object> parameters, AsyncCallback callback, object state) { Func<ActionExecutedContext> endContinuation = null; BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState) { ActionExecutingContext preContext = new ActionExecutingContext(controllerContext, actionDescriptor, parameters); IAsyncResult innerAsyncResult = null; Func<Func<ActionExecutedContext>> beginContinuation = () => { innerAsyncResult = BeginInvokeActionMethod(controllerContext, actionDescriptor, parameters, asyncCallback, asyncState); return () => new ActionExecutedContext(controllerContext, actionDescriptor, false /* canceled */, null /* exception */) { Result = EndInvokeActionMethod(innerAsyncResult) }; }; // need to reverse the filter list because the continuations are built up backward Func<Func<ActionExecutedContext>> thunk = filters.Reverse().Aggregate(beginContinuation, (next, filter) => () => InvokeActionMethodFilterAsynchronously(filter, preContext, next)); endContinuation = thunk(); if (innerAsyncResult != null) { // we're just waiting for the inner result to complete return innerAsyncResult; } else { // something was short-circuited and the action was not called, so this was a synchronous operation SimpleAsyncResult newAsyncResult = new SimpleAsyncResult(asyncState); newAsyncResult.MarkCompleted(true /* completedSynchronously */, asyncCallback); return newAsyncResult; } }; EndInvokeDelegate<ActionExecutedContext> endDelegate = delegate(IAsyncResult asyncResult) { return endContinuation(); }; return AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, _invokeActionMethodWithFiltersTag); }
protected internal virtual IAsyncResult BeginInvokeActionMethodWithFilters( ControllerContext controllerContext, IList <IActionFilter> filters, ActionDescriptor actionDescriptor, IDictionary <string, object> parameters, AsyncCallback callback, object state ) { Func <ActionExecutedContext> endContinuation = null; BeginInvokeDelegate beginDelegate = delegate( AsyncCallback asyncCallback, object asyncState ) { AsyncInvocationWithFilters invocation = new AsyncInvocationWithFilters( this, controllerContext, actionDescriptor, filters, parameters, asyncCallback, asyncState ); const int StartingFilterIndex = 0; endContinuation = invocation.InvokeActionMethodFilterAsynchronouslyRecursive( StartingFilterIndex ); if (invocation.InnerAsyncResult != null) { // we're just waiting for the inner result to complete return(invocation.InnerAsyncResult); } else { // something was short-circuited and the action was not called, so this was a synchronous operation SimpleAsyncResult newAsyncResult = new SimpleAsyncResult(asyncState); newAsyncResult.MarkCompleted( completedSynchronously: true, callback: asyncCallback ); return(newAsyncResult); } }; EndInvokeDelegate <ActionExecutedContext> endDelegate = delegate( IAsyncResult asyncResult ) { return(endContinuation()); }; return(AsyncResultWrapper.Begin( callback, state, beginDelegate, endDelegate, _invokeActionMethodWithFiltersTag )); }