0

I have to call a restsharp ExecuteTaskAsync, I have used await while executing the API and await to complete all tasks since it runs in loop, as soon as it hits await System.Threading.Tasks.Task.WhenAll(tasksList), then no it's blocked, no response in it.

Calling Async code:

Task<IEnumerable<AsyncResponse>> responseList = AddPAsync(id, id1);

To Execute Restsharp's ExecuteTaskAsync:

public static async Task<AsyncResponse> ExecuteApiAsync(RestRequest request, string url, dynamic identifier)
{
    var restClient = new RestClient(url);
    var cancellationTokenSource = new CancellationTokenSource();
    var restResponse = await restClient.ExecuteTaskAsync(request);

    return new AsyncResponse{ RestResponse = restResponse, Identifier = identifier };
}

Preparing request and calling RestSharp's ExecuteTaskAsync:

private async Task<IEnumerable<AsyncResponse>> AddPAsync(List<Participant> participantInfo, string registrationId)
{
    foreach (var p in pinfo)
    {
        try
        {
            var request = new RestRequest(Constants.API_VERSION + Uri, Method.POST);
            request.AddHeader("Authorization", string.Format("Bearer {0}", accessToken));
            request.AddParameter(Constants.APP_JSON, JsonConvert.SerializeObject(p), ParameterType.RequestBody);

            var response = Util.ExecuteApiAsync(request, Constants.END_POINT_URL_NAME, p.Identifier);
            tasksList.Add(response);
        }
        catch (Exception ex)
        {

        }
    }

    await System.Threading.Tasks.Task.WhenAll(tasksList);
}

When it hits await Task.WhenAll then no response.
I have already tried:
`ConfigureAwait(false) - it is not working.
It is ASP.Net MVC application in sitecore.

GSerg
  • 76,472
  • 17
  • 159
  • 346
Abhishek Jain
  • 171
  • 1
  • 13
  • Should it not be `Task.WaitAll`? – Dumisani Jul 11 '19 at 07:18
  • `async` is just syntactic sugar and `await` doesn't *block*. It awaits *without blocking* for an already executing task to finish before resuming on the original synchronization context. If that task doesn't finish, `await` won't return. – Panagiotis Kanavos Jul 11 '19 at 07:19
  • 1
    @Dumisani *that* would block, while `await Task.WhenAll()` *doesn't* block – Panagiotis Kanavos Jul 11 '19 at 07:19
  • Oh, understood. – Dumisani Jul 11 '19 at 07:20
  • 1
    Well, you do not have `await` in that `Task> responseList = AddPAsync(id, id1);`, do you. Do you also `await` the `responseList` later? – GSerg Jul 11 '19 at 07:21
  • Task.WaitAll is also not working – Abhishek Jain Jul 11 '19 at 07:25
  • @AbhishekJain `Task.WaitAll` is *a blocking call*. `await Task.WhenAll` isn't. And you don't *await* the call to `AddPAsync` so there's no possibility of blocking there either. The code you posted doesn't show anything that could block, although it *does* show a possibly runaway task whose results aren't used by anyone. The action that calls `AddPAsync` would return before that method had a chance to finish. – Panagiotis Kanavos Jul 11 '19 at 07:27
  • I tried with only one request, instead of loop, that also did not work – Abhishek Jain Jul 11 '19 at 08:30
  • 1
    Do you have blocking calls anywhere *further up* your call stack? – Stephen Cleary Jul 11 '19 at 12:38
  • @StephenCleary - No this is the only async call, no where WhenAll or WaitAll is used apart from this – Abhishek Jain Jul 11 '19 at 14:51
  • @AbhishekJain: If the calling method isn't `async`, then how is this method called? – Stephen Cleary Jul 11 '19 at 15:04
  • @StephenCleary: Where AddPAsync is called, that method is not async method, AddPAsync is async method which in turn calls ExecuteApiAsync method. Anything wrong? – Abhishek Jain Jul 11 '19 at 15:45
  • 1
    @AbhishekJain Pretty much. How do you then get the result of that async `AddPAsync` method called from a non-async method? By [using `responseList.Result`](https://stackoverflow.com/q/17248680/11683)? – GSerg Jul 11 '19 at 16:02
  • @GSerg: Yes , Task> responseList = AddPAsync(id, id1); if (responseList != null) { foreach (var restResponse in responseList.Result) { – Abhishek Jain Jul 11 '19 at 16:03
  • 1
    Possible duplicate of [await works but calling task.Result hangs/deadlocks](https://stackoverflow.com/questions/17248680/await-works-but-calling-task-result-hangs-deadlocks) – GSerg Jul 11 '19 at 16:03
  • @GSerg: Yes, if i remove .Result, it does not hang, but how do i get the Result to iterate? – Abhishek Jain Jul 11 '19 at 16:24
  • 1
    By using `await responseList`. – GSerg Jul 11 '19 at 17:21
  • To answer to your next question, *"But then the calling method also has to be async"*, - that [would be correct](https://msdn.microsoft.com/en-us/magazine/jj991977.aspx). Alternatively, see https://stackoverflow.com/q/9343594/11683. – GSerg Jul 11 '19 at 17:39
  • @GSerg: Adding AsyncContext from Nito.AsyncEx worked. Thanks a lot! – Abhishek Jain Jul 12 '19 at 05:29

1 Answers1

0

Adding AsyncContext from Nito.AsyncEx worked.

Abhishek Jain
  • 171
  • 1
  • 13