I'm getting a List of Object with URL,some Data and a User Id. I want to write a function to call the different web services asynchronously and return a list of response and User Id in list. But if the URL is same then the call should be Synchronous to that particular service with different data provided in input.
Asked
Active
Viewed 292 times
0
-
What's the "different web services" you talk about? You should read [ask] and then provide a [mcve]. Perhaps if you know how to call the service synchronously then show us that code. Then we can give you more answers. – Enigmativity May 06 '19 at 11:42
1 Answers
1
You can follow the following steps - Group the list by url - pass the grouped item to separate async method - the second method would make service call as synchronously
Please refer to below code
List<Response> DoTask(List<MyObject> list)
{
var groupedList = list.GroupBy(x => x.Url)
var taskList = new List<Task<List<Response>>>();
foreach(group in GroupList)
{
taskList.Add(CallService(group.ToList()))
}
Task.WaitAll(taskList.ToArray());
return taskList.SelectMany(x => x.Result);
}
// This method would get service call of same urls only
async Task<List<Response>> CallService(List<MyObject> list)
{
var responseList = new List<Response>();
foreach(var item in list)
{
var response = new Response();
response.Result = await HttpServiceCall(MyObject object);
response.UserId = item.UserId
responseList.Add(response);
}
return responseList;
}
async Task<Response> HttpServiceCall(MyObject object)
{
//make service call
}

Manvinder Singh
- 140
- 1
- 9
-
My understanding of async/await still isn't what it should be, so I'm 99% sure I'm seeing this wrong. If you await each `HttpServiceCall` one after another in a loop, doesn't that mean that although they are asynchronous, they still execute one after the other sequentially? That's still better than if they weren't async, but wouldn't it be preferable to `.WhenAll` all of those tasks? (I'm still trying to learn this.) – Scott Hannen May 02 '19 at 12:04
-
Yes, you are right HttpServiceCall will run sequentially because all the calls are awaited and if you don't need this do asynchronously here or at any another place in application then you should define this method without async. It is not recommended to make methods async if you don't need them to because a single thread would perform better than multiple threads running sequentially. Are you asking about using WhenAll at the place of WaitAll? – Manvinder Singh May 02 '19 at 15:57
-
If I understand correctly (which is very questionable), awaiting one call, then another, then another means that as each request is made, the thread is free to do other things. When the call completes, the next one begins, and again the thread can do other things. But `WhenAll` would avoid forcing the calls to complete sequentially. https://stackoverflow.com/questions/18310996/why-should-i-prefer-single-await-task-whenall-over-multiple-awaits – Scott Hannen May 02 '19 at 16:06
-
If you are asking about using WhenAll instead of await in the loop then it would be against your question statement because you need the tasks to be run sequentially if "url" is same and with WhenAll tasks would run asynchronously. And the second thing is ,,,a single thread is better then multiple threads running sequentially because with multiple threads there would be more thread switching which is a costly task and would effect the performance – Manvinder Singh May 02 '19 at 16:20