1

Is it incorrect to use asyc await in a foreach loop? I get a WebException, The request was canceled.

The following is just a outline of the code I’m working on. Please let me know if this is not clear.

using (var client = new HttpClient()) { client.BaseAddress = new Uri("https://xxx.xxx.com/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); try { var response = await client.PostAsJsonAsync("customers/xxxx/xxx/documents", requestMessage); response.EnsureSuccessStatusCode(); var returnValue = response.Content.ReadAsAsync<ResponseMessage>(); var hasDocuments = returnValue.Result.SearchResults.Any(); if (hasDocuments) { // HTTP GET client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*")); returnValue.Result.SearchResults.ForEach(async d => { response = await client.GetAsync(d.Self + ".pdf"); }); } } catch (Exception ex) { throw; } } 

1 Answer 1

8

You are not using async/await in a foreach loop, you are using async/await with List<T>.ForEach(Action<T>). Because ForEach only accepts Action<T> (void return type) your ForEach(async d => { ... } is being processed as a async void function because that is the only way for it to be passed in as a Action<T>. This makes ForEach not wait for the previous item to finish before moving to the next item and that is what causes your error.

Use a normal foreach instead of the List<T>.ForEach method and it should work fine.

 if (hasDocuments) { // HTTP GET client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*")); foreach (var d in returnValue.Result.SearchResults) { response = await client.GetAsync(d.Self + ".pdf"); }; } 

The only time you are allowed to use async d => ... is when the function you are passing in to takes in a Func<T,Task> (See Task.Run as an example).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.