diff --git a/Src/SwqlStudio/ConnectionInfo.cs b/Src/SwqlStudio/ConnectionInfo.cs index 71345373f..e16359436 100644 --- a/Src/SwqlStudio/ConnectionInfo.cs +++ b/Src/SwqlStudio/ConnectionInfo.cs @@ -196,14 +196,14 @@ public DataTable Query(string swql) public DataTable Query(string swql, out XmlDocument queryPlan, out XmlDocument queryStats) { - EnsureConnection(); - XmlDocument tmpQueryPlan = null; // can't reference out parameter from closure XmlDocument tmpQueryStats = null; // can't reference out parameter from closure DataTable result = DoWithExceptionTranslation( delegate { + EnsureConnection(); + using (InformationServiceCommand command = new InformationServiceCommand(swql, Connection) { ApplicationTag = "SWQL Studio" }) { foreach (var param in QueryParameters) @@ -233,10 +233,11 @@ public static void DoWithExceptionTranslation(Action action) }); } - public static T DoWithExceptionTranslation(Func action) + public static T DoWithExceptionTranslation(Func action, bool retryOnConnectionError = true) { string msg; Exception inner; + bool couldRetry = false; try { @@ -254,15 +255,16 @@ public static T DoWithExceptionTranslation(Func action) } catch (FaultException ex) { - msg = (ex.InnerException != null) ? ex.InnerException.Message : ex.Message; + msg = ex.InnerException?.Message ?? ex.Message; inner = ex.InnerException ?? ex; } catch (MessageSecurityException ex) { - if (ex.InnerException != null && ex.InnerException is FaultException) + if (ex.InnerException is FaultException fault) { - msg = (ex.InnerException as FaultException).Message; - inner = ex.InnerException; + msg = fault.Message; + inner = fault; + couldRetry = fault.Code?.SubCode?.Name == "BadContextToken"; } else { @@ -270,12 +272,23 @@ public static T DoWithExceptionTranslation(Func action) inner = ex; } } + catch (CommunicationObjectFaultedException ex) + { + msg = ex.Message; + inner = ex; + couldRetry = true; + } catch (Exception ex) { msg = ex.Message; inner = ex; } + if (couldRetry && retryOnConnectionError) + { + return DoWithExceptionTranslation(action, false); + } + throw new ApplicationException(msg, inner); }