示例#1
0
 /// <summary>
 /// Invokes a web method asynchronously.
 /// </summary>
 /// <param name="methodName">Web method name.</param>
 /// <param name="asyncCallState">Call state handle.
 /// Upon return, may be used to cancel the call</param>
 /// <param name="parameters">Web method parameters.</param>
 /// <param name="callback">Callback method to process the result.</param>
 /// <seealso cref="CancelAsync(AsyncCallState)"/>
 public void InvokeAsync(
     string methodName,
     AsyncCallState asyncCallState,
     Delegate callback,
     params object[] parameters)
 {
     InvokeAsync(methodName, asyncCallState, null, callback, parameters);
 }
示例#2
0
        /// <summary>
        ///.Cancel an asynchronous call if it is not completed already.
        /// </summary>
        /// <param name="asyncCallState">Async call state.</param>
        public void CancelAsync(AsyncCallState asyncCallState)
        {
            if (asyncCallState.PendingCall == null)
            {
                return;
            }

            CancelAsync(asyncCallState.PendingCall);
            asyncCallState.PendingCall = null;
        }
示例#3
0
        /// <summary>
        /// Invokes a web method asynchronously.
        /// </summary>
        /// <param name="methodName">Web method name.</param>
        /// <param name="asyncCallState">Call state handle.
        /// Upon return, may be used to cancel the call</param>
        /// <param name="parameters">Web method parameters.</param>
        /// <param name="callback">Callback method to process the result.</param>
        /// <param name="exceptionHandler">Fail handler.</param>
        /// <seealso cref="CancelAsync(AsyncCallState)"/>
        public void InvokeAsync(
            string methodName,
            AsyncCallState asyncCallState,
            Action <Exception> exceptionHandler,
            Delegate callback,
            params object[]   parameters)
        {
#if DEBUG
            var sw = Stopwatch.StartNew();
#endif

            if (asyncCallState != null)
            {
#if DEBUG
                Debug.WriteLineIf(TS.TraceVerbose && asyncCallState.PendingCall != null,
                                  string.Format("Cancelling async call {0}/{1}",
                                                Url, methodName), TS.DisplayName);
#endif
                CancelAsync(asyncCallState);
            }

            var exceptionCallback = exceptionHandler ?? delegate(Exception ex)
            {
                if (ex is WebException)
                {
                    var webException = (WebException)ex;

                    // Request cancelled.
                    //
                    if (webException.Status == WebExceptionStatus.RequestCanceled)
                    {
                        OnWebOperationCancelled(methodName, parameters);
                        return;
                    }
                }

                // Check for retry.
                //
                if (OnWebOperationException(methodName, parameters, ex))
                {
                    InvokeAsync(methodName, asyncCallState, exceptionHandler, callback, parameters);
                }
            };

            System.Threading.SendOrPostCallback sendCallback = delegate(object obj)
            {
#if DEBUG
                Debug.WriteLineIf(TS.TraceVerbose,
                                  string.Format("Async call {0}/{1} = {2} msec.",
                                                Url, methodName, sw.ElapsedMilliseconds), TS.DisplayName);
#endif

                var ea = (InvokeCompletedEventArgs)obj;

                if (ea.Error != null)
                {
                    // Internal redirection
                    //
                    if (ea.Error is WebException && ((WebException)ea.Error).Status == WebExceptionStatus.ReceiveFailure)
                    {
                        InvokeAsync(methodName, asyncCallState, exceptionHandler, callback, parameters);
                    }
                    else
                    {
                        exceptionCallback(ea.Error);
                    }
                }
                else if (ea.Cancelled || (asyncCallState != null && ea.UserState != asyncCallState.PendingCall))
                {
                    exceptionCallback(new WebException(methodName, WebExceptionStatus.RequestCanceled));
                }
                else
                {
                    callback.DynamicInvoke(AcceptChanges(ea.Results));
                }

                if (asyncCallState != null && ea.UserState == asyncCallState.PendingCall)
                {
                    asyncCallState.PendingCall = null;
                }
            };

            object cookie = new CompoundValue(methodName, parameters);

            if (asyncCallState != null)
            {
                asyncCallState.PendingCall = cookie;
            }

            InvokeAsync(methodName, parameters, sendCallback, cookie);
        }