/// <summary> /// Delete a list of credit card rows. /// </summary> /// <param name="state">The list to delete.</param> public void DestroyRecords(object state) { List <CreditCardRow> toDeleteRows = state as List <CreditCardRow>; TradingSupportClient tradingSupportClient = new TradingSupportClient(Guardian.Properties.Settings.Default.TradingSupportEndpoint); try { int recordsPerCall = 100; TradingSupportReference.CreditCard[] orders = null; int recordTotal = 0; int recordIndex = 0; foreach (CreditCardRow workingOrderRow in toDeleteRows) { if (recordIndex == 0) { orders = new TradingSupportReference.CreditCard[ toDeleteRows.Count - recordTotal < recordsPerCall ? toDeleteRows.Count - recordTotal : recordsPerCall]; } orders[recordIndex++] = new TradingSupportReference.CreditCard() { RowId = workingOrderRow.CreditCardId, RowVersion = workingOrderRow.RowVersion }; if (recordIndex == orders.Length) { MethodResponseErrorCode response = tradingSupportClient.DeleteCreditCard(orders); if (!response.IsSuccessful) { ErrorCode error = response.Result; if (response.Errors.Length > 0) { error = response.Errors[0].ErrorCode; } if (error == ErrorCode.RecordExists) { throw new IsSettledException(string.Format("{0} is settled", workingOrderRow.OriginalAccountNumber)); } else { throw new Exception(String.Format("Server error {0}", response.Result)); } } recordTotal += recordIndex; recordIndex = 0; } } } catch (IsSettledException exception) { EventLog.Information("{0}: {1}\n{2}", exception.GetType(), exception.Message, exception.StackTrace); this.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(Application.Current.MainWindow, "Cannot delete Credit Card: " + exception.Message, Application.Current.MainWindow.Title))); } catch (Exception exception) { // Any issues trying to communicate to the server are logged. EventLog.Error("{0}: {1}\n{2}", exception.GetType(), exception.Message, exception.StackTrace); this.Dispatcher.BeginInvoke(new Action(() => MessageBox.Show(Application.Current.MainWindow, "Cannot delete Credit Card", Application.Current.MainWindow.Title))); } finally { if (tradingSupportClient != null && tradingSupportClient.State == CommunicationState.Opened) { tradingSupportClient.Close(); } } }