Exemplo n.º 1
0
        public void ThrowException(WCFInvocationMethod clientInvocationMethod, WCFInvocationMethod serviceInvocationMethod, bool asyncThrowOnBegin)
        {
            if (_wcfClient == null)
            {
                throw new InvalidOperationException("WCF Client not instantiated");
            }

            if (_needToPauseForWarmup)
            {
                PauseForWarmup();
            }

            try
            {
                string result = null;
                switch (clientInvocationMethod)
                {
                case WCFInvocationMethod.Sync:
                    switch (serviceInvocationMethod)
                    {
                    case WCFInvocationMethod.Sync:
                        result = _wcfClient.Sync_SyncThrowException();
                        break;

                    case WCFInvocationMethod.BeginEndAsync:
                        result = asyncThrowOnBegin
                                    ? _wcfClient.Sync_AsyncThrowExceptionAtStart()
                                    : _wcfClient.Sync_AsyncThrowExceptionAtEnd();
                        break;

                    case WCFInvocationMethod.TAPAsync:
                        result = _wcfClient.Sync_TAPThrowException();
                        break;

                    default:
                        throw new NotImplementedException($"Client/Service Invocation Method Combo {clientInvocationMethod}/{serviceInvocationMethod}");
                    }
                    break;

                case WCFInvocationMethod.BeginEndAsync:
                    switch (serviceInvocationMethod)
                    {
                    case WCFInvocationMethod.Sync:
                    {
                        var asyncResult = _wcfClient.Begin_SyncThrowException(null, null);
                        result = _wcfClient.End_SyncThrowException(asyncResult);
                    }
                    break;

                    case WCFInvocationMethod.BeginEndAsync:
                    {
                        if (asyncThrowOnBegin)
                        {
                            var asyncResult = _wcfClient.Begin_AsyncThrowExceptionAtStart(null, null);
                            result = _wcfClient.End_AsyncThrowExceptionAtStart(asyncResult);
                        }
                        else
                        {
                            var asyncResult = _wcfClient.Begin_AsyncThrowExceptionAtEnd(null, null);
                            result = _wcfClient.End_AsyncThrowExceptionAtEnd(asyncResult);
                        }
                    }
                    break;

                    case WCFInvocationMethod.TAPAsync:
                    {
                        var asyncResult = _wcfClient.Begin_TAPThrowException(null, null);
                        result = _wcfClient.End_TAPThrowException(asyncResult);
                    }
                    break;

                    default:
                        throw new NotImplementedException($"Client/Service Invocation Method Combo {clientInvocationMethod}/{serviceInvocationMethod}");
                    }

                    break;

                case WCFInvocationMethod.TAPAsync:
                    switch (serviceInvocationMethod)
                    {
                    case WCFInvocationMethod.Sync:
                    {
                        result = _wcfClient.TAP_SyncThrowException().Result;
                    }
                    break;

                    case WCFInvocationMethod.BeginEndAsync:
                    {
                        result = asyncThrowOnBegin
                                        ? _wcfClient.TAP_AsyncThrowExceptionAtStart().Result
                                        : _wcfClient.TAP_AsyncThrowExceptionAtEnd().Result;
                    }
                    break;

                    case WCFInvocationMethod.TAPAsync:
                    {
                        result = _wcfClient.TAP_TAPThrowException().Result;
                    }
                    break;

                    default:
                        throw new NotImplementedException($"Client/Service Invocation Method Combo {clientInvocationMethod}/{serviceInvocationMethod}");
                    }
                    break;

                case WCFInvocationMethod.EventBasedAsync:
                    switch (serviceInvocationMethod)
                    {
                    case WCFInvocationMethod.Sync:
                        using (var wait = new ManualResetEvent(false))
                        {
                            _wcfClient.Event_ThrowException_Completed += (e, a) =>
                            {
                                if (a.Error != null)
                                {
                                    result = "Handled Expected WCF Exception";
                                }
                                else
                                {
                                    throw new Exception("Expected WCF Error didn't occur");
                                }

                                wait.Set();
                            };
                            _wcfClient.Event_SyncThrowException();
                            wait.WaitOne(TimeSpan.FromSeconds(20));
                        }
                        break;

                    default:
                        throw new NotImplementedException($"Client/Service Invocation Method Combo {clientInvocationMethod}/{serviceInvocationMethod}");
                    }
                    break;

                default:
                    throw new NotImplementedException($"Client Invocation Method {clientInvocationMethod} Not supported");
                }

                Logger.Info($"Result: {result ?? "<NULL>"}");
            }
            catch (FaultException)

            {
                Logger.Info("Ignoring WCF Fault Exception!");
            }
            catch (AggregateException aggEx)
            {
                if (!(aggEx.InnerException is FaultException) && !(aggEx.InnerException is ProtocolException))
                {
                    throw;
                }

                Logger.Info($"Ignoring AggregateException -> WCF {aggEx.InnerException.GetType()} Exception!");
            }
            catch (TargetInvocationException tgtEx)
            {
                if (!(tgtEx.InnerException is FaultException <ExceptionDetail>))
                {
                    throw;
                }


                Logger.Info("Ignoring TergetInvocationException -> WCF Fault Exception<ExceptionDetail>!");
            }
            catch (ProtocolException)
            {
                Logger.Info("Ignoring ProtocolException");
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }