private bool HandleExceptions(Action action, Action onException, RemoteScenarioInfo info) { var strErrPrefix = "Ошибка во время соединения с удаленным сценарием"; try { action?.Invoke(); return(true); } catch (Exception e) { //crutch if (e is AggregatedCommunicationException) { Log.InfoFormat(strErrPrefix + ". {0}; [{2}], удаленное ID:[{1}], [{3}]", e.Message, info.Name, info.ScenarioId, Credentials.GetAddress()); } else if ( SystemUtils.IsFaultExceptionHasCode(e, ServiceFaultCodes.ObjectNotFound) || SystemUtils.IsFaultExceptionHasCode(e, ServiceFaultCodes.DecryptionError) || SystemUtils.IsFaultExceptionHasCode(e, ServiceFaultCodes.ObjectAccessDenied) || SystemUtils.IsFaultExceptionHasCode(e, ServiceFaultCodes.AccessDenied)) { Log.InfoFormat(strErrPrefix + ". " + e.Message + "; [{0}], [{2}], удаленное ID:[{1}], [{3}]", info.Name, info.ScenarioId, Credentials.GetAddress(), e.InnerException?.Message); } else { Log.WarnFormat(e, strErrPrefix + ". Unrecognized exception; [{0}], [{2}], удаленное ID:[{1}], [{3}]", info.Name, info.ScenarioId, Credentials.GetAddress(), e.InnerException?.Message); } onException?.Invoke(); return(false); } }
private bool HandleExceptions(Action action) { var cancellationToken = _operationCancellationTokenSource.Token; bool success = false; try { if (_credentials != null && (_serviceClient?.IsClosedOrFaulted ?? false)) { _serviceClient = ClientsManager.Create(_credentials.Value); } if (ConnectionState == ManagerConnectionState.Disconnected) { ConnectionState = ManagerConnectionState.Connecting; } action(); success = true; if (ConnectionState != ManagerConnectionState.Connected) { ConnectionState = ManagerConnectionState.Connected; ConnectionRestored?.Invoke(); } } catch (System.Exception e) { if (!cancellationToken.IsCancellationRequested) { if (SystemUtils.IsFaultExceptionHasCode(e, ServiceFaultCodes.AccessDenied)) { LoginOrPasswordInvalid?.Invoke(); } //if data is wrong or secretKey.Length is wrong else if ( SystemUtils.IsFaultExceptionHasCode(e, ServiceFaultCodes.DecryptionError) || e is SerializationException || e is DecryptException || e.Message == "Key length not 128/192/256 bits.") { SecretCodeInvalid?.Invoke(); } else if (ConnectionState != ManagerConnectionState.Connected) { ConnectionLost?.Invoke(); } else { ConnectionError?.Invoke(); } ConnectionState = ManagerConnectionState.Disconnected; success = false; } } if (cancellationToken.IsCancellationRequested) { return(false); } return(success); }
private T Handle <T>(Func <UserBase, T> function, [CallerMemberName] string memberName = "") { try { WarningHandler.DebugFormat("[{0}] execution started", memberName); var currentUser = GetCurrentUser(); var result = function(currentUser); if (result is Array) { WarningHandler.DebugFormat("[{0}] result: [{1}] items", memberName, ((Array)(object)result).Length); } else if (result is IList) { WarningHandler.DebugFormat("[{0}] result: [{1}] items", memberName, ((IList)result).Count); } return(result); } catch (Exception e) { if (SystemUtils.IsFaultExceptionHasCode(e, ServiceFaultCodes.AccessDenied) || SystemUtils.IsFaultExceptionHasCode(e, ServiceFaultCodes.ObjectAccessDenied) || SystemUtils.IsFaultExceptionHasCode(e, ServiceFaultCodes.ObjectNotFound)) { WarningHandler.WarnFormat("[{0}] execution error. {1}", memberName, e.Message); //write only message throw e; } else if (e is DecryptException) { WarningHandler.WarnFormat("[{0}] execution error. {1}", memberName, e.Message); //write only message throw new FaultException(e.Message, new FaultCode(ServiceFaultCodes.DecryptionError)); } else if (e is ScenarioExecutionException executionError) { if (executionError.ErrorType == ScenarioExecutionError.AccessDenied) { ThrowUnauthorizedAccessException(); } } else { WarningHandler.ErrorFormat(e, "[{0}] execution error", memberName); //unrecognized exception; write fully } } finally { WarningHandler.DebugFormat("[{0}] executed", memberName); } return(default(T)); }
private IServer CreateProxyClient(ConnectionCredentials credentials) { var channelFactory = CreateClient(credentials); var connection = channelFactory.CreateChannel(); IServer connectionProxy = null; var isFailed = false; connectionProxy = ProxyObject.Create(connection, (args) => { Log.DebugFormat("Service method entered: [{0}]", args.MethodName); var result = args.DefaultReturnValue; try { result = args.Run(); if (!isFailed) { isFailed = true; ConnectionStateChanged?.Invoke(this, new ConnectionStateChangedEventArgs(connectionProxy, isFailed, credentials)); } } catch (Exception e) { Log.DebugFormat("Service method error: [{0}]; {1}", args.MethodName, e.InnerException.Message); var targetException = e.InnerException; if (isFailed && !SystemUtils.IsFaultExceptionHasCode(targetException, ServiceFaultCodes.ObjectNotFound) && !SystemUtils.IsFaultExceptionHasCode(targetException, ServiceFaultCodes.ObjectAccessDenied)) { isFailed = false; ConnectionStateChanged?.Invoke(this, new ConnectionStateChangedEventArgs(connectionProxy, isFailed, credentials)); } //if communication exception if (targetException is System.ServiceModel.ServerTooBusyException || targetException is System.ServiceModel.ServiceActivationException || targetException is System.ServiceModel.AddressAccessDeniedException || targetException is System.ServiceModel.AddressAlreadyInUseException || targetException is System.ServiceModel.ChannelTerminatedException || targetException is System.ServiceModel.CommunicationObjectAbortedException || targetException is System.ServiceModel.EndpointNotFoundException || targetException is System.ServiceModel.QuotaExceededException || targetException is System.ServiceModel.CommunicationObjectFaultedException || targetException.GetType().Equals(typeof(CommunicationException)) || targetException is System.TimeoutException || channelFactory.State == CommunicationState.Closed || channelFactory.State == CommunicationState.Closing || channelFactory.State == CommunicationState.Faulted) { AggregatedCommunicationException.Throw(targetException); } else if (targetException is MessageSecurityException && targetException.InnerException != null) { throw targetException.InnerException; } else { throw targetException; } } finally { Log.DebugFormat("Service method exit: [{0}]", args.MethodName); } return(result); }); return(connectionProxy); }