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;
        }
        protected virtual void SendRequest()
        {
            //throw new NotImplementedException("Override SendRequest and implement");
            WSHttpBinding binding = new WSHttpBinding();

            binding.SendTimeout    = new TimeSpan(1, 0, 0);
            binding.ReceiveTimeout = new TimeSpan(1, 0, 0);
            EndpointAddress address = new EndpointAddress("http://localhost:3344/ProxyServer.svc");
            ChannelFactory <IProxyServer> channelFactory = new ChannelFactory <IProxyServer>(binding, address);
            IProxyServer service = channelFactory.CreateChannel();

            _result = service.ProcessRequest(_request);
            EndRequest(null);
        }
Ejemplo n.º 3
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);
        }