private void ExternalInvoiceServiceManager_EventExternalInvoiceServiceFailed(object sender, System.EventArgs e) { var serviceManagerArgs = e as ServiceManagerArgs; consecutiveErrors = consecutiveErrors + 1; if (consecutiveErrors > 3) { var failOverService = new FailoverInvoiceService(); var failOverInvoices = failOverService.GetInvoices(serviceManagerArgs.supplier.Id); this.spendSummary = this.externalInvoiceServiceManager.TryGetSpendSummaryFromFailoverService(serviceManagerArgs.supplier, failOverInvoices); } else { this.spendSummary = this.externalInvoiceServiceManager.TryGetSpendSummaryFromExternalService(serviceManagerArgs.supplier, ExternalInvoiceService.GetInvoices); } }
private void ExternalInvoiceServiceManager_EventDataNotRefreshed(object sender, System.EventArgs e) { var serviceManagerArgs = e as ServiceManagerArgs; consecutiveErrors = consecutiveErrors + 1; if (consecutiveErrors > 6) { _autoEvent = new AutoResetEvent(false); tm = new Timer(Execute, _autoEvent, 1000, 1000); } else { var failOverService = new FailoverInvoiceService(); var failOverInvoices = failOverService.GetInvoices(serviceManagerArgs.supplier.Id); this.spendSummary = this.externalInvoiceServiceManager.TryGetSpendSummaryFromFailoverService(serviceManagerArgs.supplier, failOverInvoices); } }
public SpendSummary GetTotalSpend() { var watch = Stopwatch.StartNew(); var tokenSource = new CancellationTokenSource(); CancellationToken ct = tokenSource.Token; int counter = 1; ct.ThrowIfCancellationRequested(); bool timeOut = true; Task <ExternalInvoice[]> tsk = new Task <ExternalInvoice[]>(() => ExternalInvoiceService.GetInvoices(_supplier.Id.ToString()), ct); tsk.Start(); while (timeOut && counter <= 3) { if (tsk.IsCompleted) { timeOut = false; break; } if (!(tsk.IsCompleted) && watch.ElapsedMilliseconds >= 30000) { tokenSource.Cancel(); } if (ct.IsCancellationRequested) { try { timeOut = false; counter++; ct.ThrowIfCancellationRequested(); } catch { } finally { tokenSource = new CancellationTokenSource(); ct = tokenSource.Token; watch.Restart(); tsk = new Task <ExternalInvoice[]>(() => ExternalInvoiceService.GetInvoices(_supplier.Id.ToString()), tokenSource.Token); tsk.Start(); } } } ExternalInvoice[] invoices = tsk.Result; if (invoices.Count() == 0) { FailoverInvoiceService failoverInvoiceService = new FailoverInvoiceService(); FailoverInvoiceCollection collection = failoverInvoiceService.GetInvoices(_supplier.Id); invoices = collection.Invoices; } var sd = (from supplier in invoices group supplier by supplier.Year into eGroup select new SpendDetail() { Year = eGroup.Key, TotalSpend = eGroup.Sum(x => x.TotalAmount) }); return(new SpendSummary() { Name = _supplier.Name, Years = sd.ToList() }); }