public async Task <double> CalculateTotalTravelDistanceAsync(int itineraryId) { var itinerary = await _dataStore.GetItinaryAsync(itineraryId); if (itinerary == null) { throw new InvalidOperationException(); } double result = 0; for (int i = 0; i < itinerary.Waypoints.Count - 1; i++) { result = result + _distanceCalculator.GetDistanceAsync(itinerary.Waypoints[i], itinerary.Waypoints[i + 1]).Result; //.Result is a blocking call in an async method //await is missing //result is not added up in a thread-safe manner } return(result); }
/// <summary> /// Calculates the total distance traveled across all waypoints in a customers itinerary. /// </summary> /// <param name="itineraryId">The identifier of the itinerary</param> /// <returns>The total distance traveled.</returns> public async Task <double> CalculateTotalTravelDistanceAsync(int itineraryId) { var itinerary = await _dataStore.GetItinaryAsync(itineraryId); if (itinerary == null) { throw new InvalidOperationException(); } double result = 0; for (int i = 0; i < itinerary.Waypoints.Count - 1; i++) { result = result + _distanceCalculator.GetDistanceAsync(itinerary.Waypoints[i], itinerary.Waypoints[i + 1]).Result; } return(result); }