protected void EndRequest(IAsyncResult asyncResult)
        {
            for (int i = 0; i < _request.Actions.Count; i++)
            {
                if (_result[i].ReturnValue is Exception && _request.StopOnError)
                {
                    throw (_result[i].ReturnValue as Exception);
                }

                //if (_request.Actions[i].Target != null)
                //{
                //    // Auto-restore the object data
                //    //_request.Actions[i].Target.Restore(_result.Data[i] as DataTable);
                //}

                if (_request.Actions[i].OnComplete != null)
                {
                    _request.Actions[i].OnComplete();
                }
            }

            if (_onComplete != null)
            {
                _onComplete();
            }

            _request = null;
            _result  = null;
        }
Пример #2
0
 public ProxyResult(ProxyRequest request)
 {
     _data = new ProxyResultActionData[request.Actions.Count];
     for (int i = 0; i < _data.Length; i++)
     {
         _data[i] = new ProxyResultActionData(i);
     }
 }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        private ProxyRequest BeginRequest(bool async)
        {
            if (_request != null)
            {
                throw new InvalidOperationException("Request already in progress");
            }

            _request      = new ProxyRequest(async);
            _request.Send = new Action(this.SendRequest);
            return(_request);
        }
Пример #4
0
        public ProxyResult ProcessRequest(ProxyRequest request)
        {
            _instances.Add(OperationContext.Current, this);

            // Proxy result used to store the results of the actions
            ProxyResult result = new ProxyResult(request);

            _result = result;

            // Action loop
            for (int currentIndex = 0; currentIndex < request.Actions.Count; currentIndex++)
            {
                ProxyRequestAction action = request.Actions[currentIndex];
                CurrentAction = action;

                try
                {
                    // Replace proxy object parameters with actual values (from previous actions)
                    for (int p = 0; p < action.Parameters.Length; p++)
                    {
                        if (action.Parameters[p] is IDataBoundObject)
                        {
                            IDataBoundObject o = (IDataBoundObject)action.Parameters[p];
                            if (o.ProxyActionIndex < currentIndex)
                            {
                                // Parameter is a proxy object referencing a previous step, so replace with that step's result
                                action.Parameters[p] = result[o.ProxyActionIndex].ReturnValue;
                            }
                            else
                            {
                                throw new InvalidOperationException(String.Format("Cannot execute action #{0} because it requires the result of action #{1}", currentIndex, o.ProxyActionIndex));
                            }
                        }
                    }

                    // Get the method to invoke
                    Type targetType = action.Caller != null?action.Caller.GetType() : Type.GetType(action.MethodType);

                    MethodInfo method = targetType.GetMethod(action.MethodName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);

                    // Perform the action and store the result
                    object val = method.Invoke(action.Caller, action.Parameters);
                    result[currentIndex].ReturnValue = val;
                }
                catch (Exception ex)
                {
                    result[currentIndex].ReturnValue = ex is TargetInvocationException ?
                                                       ((TargetInvocationException)ex).InnerException :
                                                       ex;

                    // If request is specified as a transaction, break execution
                    if (request.StopOnError)
                    {
                        break;
                    }
                }
            }

            // Done, remove from static results and return
            _result            = null;
            this.CurrentAction = null;
            _instances.Remove(OperationContext.Current);

            return(result);
        }