Пример #1
0
        private static IAsyncResult MakeSynchronousAsyncResult(AsyncCallback callback, object state)
        {
            MvcAsyncResult asyncResult = new MvcAsyncResult(state);

            asyncResult.MarkCompleted(true, callback);

            return(asyncResult);
        }
        public override IAsyncResult BeginExecute(ControllerContext context,
                                                  IDictionary <string, object> parameters, AsyncCallback callback, object state)
        {
            Precondition.Require(context, () => Error.ArgumentNull("context"));
            Precondition.Require(parameters, () => Error.ArgumentNull("parameters"));

            AsyncManager asyncManager = GetAsyncManager(context.Controller);

            BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState) {
                object[] parameterValues = _entryMethod.GetParameters()
                                           .Select(p => ExtractParameter(p, parameters, _entryMethod))
                                           .ToArray();

                TriggerListener listener    = new TriggerListener();
                MvcAsyncResult  asyncResult = new MvcAsyncResult(asyncState);

                Trigger finishTrigger = listener.CreateTrigger();
                asyncManager.Finished += delegate {
                    finishTrigger.Fire();
                };
                asyncManager.OutstandingOperations.Increment();
                listener.SetContinuation(() => ThreadPool.QueueUserWorkItem(_ =>
                                                                            asyncResult.MarkCompleted(false, asyncCallback)));

                DispatcherCache.GetDispatcher(_entryMethod)
                .Execute(context.Controller, parameterValues);

                asyncManager.OutstandingOperations.Decrement();
                listener.Activate();

                return(asyncResult);
            };

            EndInvokeDelegate <object> endDelegate = delegate(IAsyncResult asyncResult) {
                object[] parameterValues = _completedMethod.GetParameters()
                                           .Select(p => ExtractParameter(p, parameters, _completedMethod))
                                           .ToArray();

                return(DispatcherCache.GetDispatcher(_completedMethod)
                       .Execute(context.Controller, parameterValues));
            };

            return(AsyncResultWrapper.Begin(callback, state, beginDelegate,
                                            endDelegate, _executeTag, asyncManager.Timeout));
        }
Пример #3
0
        public static IAsyncResult BeginSynchronous <TResult>(AsyncCallback callback,
                                                              object state, Func <TResult> func, object tag)
        {
            BeginInvokeDelegate beginDelegate = (asyncCallback, asyncState) => {
                MvcAsyncResult inner = new MvcAsyncResult(asyncState);
                inner.MarkCompleted(true, asyncCallback);

                return(inner);
            };

            EndInvokeDelegate <TResult> endDelegate = _ => {
                return(func());
            };

            WrappedAsyncResult <TResult> result =
                new WrappedAsyncResult <TResult>(beginDelegate, endDelegate, tag);

            result.Begin(callback, state, Timeout.Infinite);

            return(result);
        }
Пример #4
0
        protected virtual IAsyncResult BeginInvokeActionFilters(ControllerContext context,
                                                                ActionDescriptor action, ICollection <IActionFilter> filters, IDictionary <string, object> parameters,
                                                                AsyncCallback callback, object state)
        {
            Func <ActionExecutedContext> endContinuation = null;

            BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState) {
                ActionExecutionContext preContext       = new ActionExecutionContext(context, action);
                IAsyncResult           innerAsyncResult = null;

                Func <Func <ActionExecutedContext> > beginContinuation = () => {
                    innerAsyncResult = BeginInvokeActionMethod(context, action, parameters, asyncCallback, asyncState);
                    return(() => new ActionExecutedContext(preContext, null)
                    {
                        Result = EndInvokeActionMethod(innerAsyncResult)
                    });
                };
                Func <Func <ActionExecutedContext> > thunk = filters.Reverse().Aggregate(beginContinuation,
                                                                                         (next, filter) => () => InvokeActionFilterAsynchronously(filter, preContext, next));
                endContinuation = thunk();

                if (innerAsyncResult != null)
                {
                    return(innerAsyncResult);
                }
                else
                {
                    MvcAsyncResult newAsyncResult = new MvcAsyncResult(asyncState);
                    newAsyncResult.MarkCompleted(true, asyncCallback);

                    return(newAsyncResult);
                }
            };
            EndInvokeDelegate <ActionExecutedContext> endDelegate = delegate(IAsyncResult asyncResult) {
                return(endContinuation());
            };

            return(AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, _invokeMethodFiltersTag));
        }
		public override IAsyncResult BeginExecute(ControllerContext context,
			IDictionary<string, object> parameters, AsyncCallback callback, object state)
		{
			Precondition.Require(context, () => Error.ArgumentNull("context"));
			Precondition.Require(parameters, () => Error.ArgumentNull("parameters"));

			AsyncManager asyncManager = GetAsyncManager(context.Controller);

			BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState) {
				object[] parameterValues = _entryMethod.GetParameters()
					.Select(p => ExtractParameter(p, parameters, _entryMethod))
					.ToArray();

				TriggerListener listener = new TriggerListener();
				MvcAsyncResult asyncResult = new MvcAsyncResult(asyncState);

				Trigger finishTrigger = listener.CreateTrigger();
				asyncManager.Finished += delegate {
					finishTrigger.Fire();
				};
				asyncManager.OutstandingOperations.Increment();
				listener.SetContinuation(() => ThreadPool.QueueUserWorkItem(_ => 
					asyncResult.MarkCompleted(false, asyncCallback)));

				DispatcherCache.GetDispatcher(_entryMethod)
					.Execute(context.Controller, parameterValues);

				asyncManager.OutstandingOperations.Decrement();
				listener.Activate();
				
				return asyncResult;
			};

			EndInvokeDelegate<object> endDelegate = delegate(IAsyncResult asyncResult) {
				object[] parameterValues = _completedMethod.GetParameters()
					.Select(p => ExtractParameter(p, parameters, _completedMethod))
					.ToArray();

				return DispatcherCache.GetDispatcher(_completedMethod)
					.Execute(context.Controller, parameterValues);
			};

			return AsyncResultWrapper.Begin(callback, state, beginDelegate, 
				endDelegate, _executeTag, asyncManager.Timeout);
		}
		private static IAsyncResult MakeSynchronousAsyncResult(AsyncCallback callback, object state)
		{
			MvcAsyncResult asyncResult = new MvcAsyncResult(state);
			asyncResult.MarkCompleted(true, callback);

			return asyncResult;
		}
		protected virtual IAsyncResult BeginInvokeActionFilters(ControllerContext context,
			ActionDescriptor action, ICollection<IActionFilter> filters, IDictionary<string, object> parameters,
			AsyncCallback callback, object state)
		{
			Func<ActionExecutedContext> endContinuation = null;

			BeginInvokeDelegate beginDelegate = delegate(AsyncCallback asyncCallback, object asyncState) {
				ActionExecutionContext preContext = new ActionExecutionContext(context, action);
				IAsyncResult innerAsyncResult = null;

				Func<Func<ActionExecutedContext>> beginContinuation = () => {
					innerAsyncResult = BeginInvokeActionMethod(context, action, parameters, asyncCallback, asyncState);
					return () => new ActionExecutedContext(preContext, null) {
						Result = EndInvokeActionMethod(innerAsyncResult)
					};
				};
				Func<Func<ActionExecutedContext>> thunk = filters.Reverse().Aggregate(beginContinuation,
					(next, filter) => () => InvokeActionFilterAsynchronously(filter, preContext, next));
				endContinuation = thunk();

				if (innerAsyncResult != null)
				{
					return innerAsyncResult;
				}
				else
				{
					MvcAsyncResult newAsyncResult = new MvcAsyncResult(asyncState);
					newAsyncResult.MarkCompleted(true, asyncCallback);

					return newAsyncResult;
				}
			};
			EndInvokeDelegate<ActionExecutedContext> endDelegate = delegate(IAsyncResult asyncResult) {
				return endContinuation();
			};
			return AsyncResultWrapper.Begin(callback, state, beginDelegate, endDelegate, _invokeMethodFiltersTag);
		}