private static Exception Unwrap(Exception ex) { return ex is TargetInvocationException && ex.InnerException != null ? ex.InnerException : ex; }