public static ExchangeRateOutputModel GetExchangeRateFoDateSetStatistics(string commaSeparatedDates, string baseCurrency, string targetCurrency) { List <DateTime?> dateSetList = commaSeparatedDates.GetDateSetArray(); List <ExchangeRateDateModel> exchangeRateDateModelList = new List <ExchangeRateDateModel>(); Parallel.For(0, dateSetList.Count, new ParallelOptions { MaxDegreeOfParallelism = 50 }, (i, state) => { int retryCounter = 0; retry: try { ExchangeRateRatePerDateServiceResponseModel exchangeRateHistory = ExchangeRateService.GetExchangeRateForSelectedDate(dateSetList[i].Value.ToString("yyyy-MM-dd"), baseCurrency, targetCurrency).Result; exchangeRateDateModelList.Add(new ExchangeRateDateModel { Date = dateSetList[i].Value.ToString("yyyy-MM-dd"), Rate = exchangeRateHistory.rates.Values.FirstOrDefault() }); } catch (Exception exService) { retryCounter++; if (retryCounter <= 2) { goto retry; } else { state.Stop(); throw exService; } } }); if (exchangeRateDateModelList.Count == dateSetList.Count) { exchangeRateDateModelList = exchangeRateDateModelList.OrderBy(x => x.Rate).ToList(); return(GetCurrencyStatisticsDataLogic(exchangeRateDateModelList)); } else { return(null); } }