Ejemplo n.º 1
0
        public DispatchResult Dispatch(Request request, string networkName)
        {
            int            retries        = 0;
            DispatchResult?dispatchResult = null;

            while (dispatchResult == null)
            {
                Exception exception = null;

                try
                {
                    retries++;
                    object result = InvokeRequest(request);
                    try
                    {
                        // Invoke return callback
                        request.Result = result;
                        if (request.ServiceAgent != null)
                        {
                            ThreadPoolInvoker threadInvoker = new ThreadPoolInvoker(request);
                            threadInvoker.ReturnResult();
                            //request.ServiceAgent.OnRemoteCallReturned(this, new RemoteCallReturnEventArgs(request));
                        }

                        InvokeReturnCallback(request, result);
                    }
                    catch (Exception ex)
                    {
                        if (ex is TargetInvocationException)
                        {
                            throw new ReturnCallbackException(ReturnCallbackFailed + ex.Message, ex.InnerException);
                        }
                        else
                        {
                            throw new ReturnCallbackException(ReturnCallbackFailed + ex.Message, ex);
                        }
                    }
                    dispatchResult = DispatchResult.Succeeded;
                }

                catch (WebException ex)
                {
                    switch (ex.Status)
                    {
                    //At server side should be the same as a failure
                    case WebExceptionStatus.NameResolutionFailure:
                    case WebExceptionStatus.ProxyNameResolutionFailure:
                    case WebExceptionStatus.ProtocolError:
                    case WebExceptionStatus.ServerProtocolViolation:
                    case WebExceptionStatus.TrustFailure:
                        exception = ex;
                        break;

                    //Otherwise should be handled out of the dispatcher.
                    default:
                        throw;
                    }
                }

                catch (Exception ex)
                {
                    exception = ex;
                    if (ex is TargetInvocationException)
                    {
                        exception = ex.InnerException;
                    }
                }

                if (exception != null)
                {
                    try
                    {
                        //Invoke the exception callback
                        if (request.ServiceAgent != null)
                        {
                            //ThreadPoolInvoker threadInvoker = new ThreadPoolInvoker(request, exception);
                            //threadInvoker.ReturnFailure();
                            request.ServiceAgent.OnRemoteCallFailure(this, new RemoteCallExceptionEventArgs(request, exception));
                        }

                        switch (InvokeExceptionRequest(request, exception))
                        {
                        case OnExceptionAction.Dismiss:
                            dispatchResult = DispatchResult.Failed;
                            break;

                        case OnExceptionAction.Retry:
                            if (retries >= request.Behavior.MaxRetries)
                            {
                                dispatchResult = DispatchResult.Failed;
                            }
                            break;
                        }
                    }
                    catch
                    {
                        dispatchResult = DispatchResult.Failed;
                    }
                }
            }

            return((DispatchResult)dispatchResult);
        }
        public DispatchResult Dispatch(Request request, string networkName)
        {
            int retries = 0;
            DispatchResult? dispatchResult = null;

            while (dispatchResult == null)
            {
                Exception exception = null;

                try
                {
                    retries++;
                    object result = InvokeRequest(request);
                    try
                    {
                        // Invoke return callback
                        request.Result = result;
                        if (request.ServiceAgent != null)
                        {
                            ThreadPoolInvoker threadInvoker = new ThreadPoolInvoker(request);
                            threadInvoker.ReturnResult();
                            //request.ServiceAgent.OnRemoteCallReturned(this, new RemoteCallReturnEventArgs(request));
                        }

                        InvokeReturnCallback(request, result);
                    }
                    catch (Exception ex)
                    {
                        if (ex is TargetInvocationException)
                            throw new ReturnCallbackException(ReturnCallbackFailed + ex.Message, ex.InnerException);
                        else
                            throw new ReturnCallbackException(ReturnCallbackFailed + ex.Message, ex);
                    }
                    dispatchResult = DispatchResult.Succeeded;
                }

                catch (WebException ex)
                {
                    switch (ex.Status)
                    {
                        //At server side should be the same as a failure
                        case WebExceptionStatus.NameResolutionFailure:
                        case WebExceptionStatus.ProxyNameResolutionFailure:
                        case WebExceptionStatus.ProtocolError:
                        case WebExceptionStatus.ServerProtocolViolation:
                        case WebExceptionStatus.TrustFailure:
                            exception = ex;
                            break;

                        //Otherwise should be handled out of the dispatcher.
                        default:
                            throw;
                    }
                }

                catch (Exception ex)
                {
                    exception = ex;
                    if (ex is TargetInvocationException)
                        exception = ex.InnerException;
                }

                if (exception != null)
                {
                    try
                    {
                        //Invoke the exception callback
                        if (request.ServiceAgent != null)
                        {
                            //ThreadPoolInvoker threadInvoker = new ThreadPoolInvoker(request, exception);
                            //threadInvoker.ReturnFailure();
                            request.ServiceAgent.OnRemoteCallFailure(this, new RemoteCallExceptionEventArgs(request, exception));
                        }

                        switch (InvokeExceptionRequest(request, exception))
                        {
                            case OnExceptionAction.Dismiss:
                                dispatchResult = DispatchResult.Failed;
                                break;
                            case OnExceptionAction.Retry:
                                if (retries >= request.Behavior.MaxRetries)
                                    dispatchResult = DispatchResult.Failed;
                                break;
                        }
                    }
                    catch
                    {
                        dispatchResult = DispatchResult.Failed;
                    }
                }
            }

            return (DispatchResult)dispatchResult;
        }