Last active
October 18, 2016 07:11
-
-
Save afrikaan-official/9b405371417cbf0c30fa6564d6e0969e to your computer and use it in GitHub Desktop.
csharp paralel requests.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static async Task<string> CalculateDistance(string origins, string destination) | |
{ | |
string url = string.Format(urlBase, origins, destination); | |
RestClient client = new RestClient(url); | |
var request = new RestRequest(); | |
var response = await client.ExecuteGetTaskAsync(request); | |
DistanceModel model = JsonConvert.DeserializeObject<DistanceModel>(response.Content); | |
var firstOrDefault = model.Rows.FirstOrDefault(); | |
Elements orDefault = firstOrDefault?.Elements.FirstOrDefault(); | |
if (orDefault != null) | |
return orDefault.Duration.Text; | |
return string.Empty; | |
} | |
public async Task<HttpResponseMessage> CalculateParallel(DistanceCalculationModel model) | |
{ | |
HttpResponseMessage message; | |
var allTasks = from station in model.Stations | |
select DistanceMatrixCalculator.CalculateDistance(model.Origin,station.Lat+","+station.Lng); | |
string[] results = await Task.WhenAll(allTasks); | |
message = Request.CreateResponse(HttpStatusCode.OK, results); | |
return message; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment