Skip to main content
added 1 character in body
Source Link
Scott Chamberlain
  • 128k
  • 37
  • 299
  • 448

You are not using async/await in a foreach loop, you are using async/await with List<T>.ForEach(Action<T>). Because ForEeachForEach 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).

You are not using async/await in a foreach loop, you are using async/await with List<T>.ForEach(Action<T>). Because ForEeach 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).

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).

added 151 characters in body
Source Link
Scott Chamberlain
  • 128k
  • 37
  • 299
  • 448

You are not using async/await in a List<T>.ForEeachforeach is the thing that does not supportloop, you are using async/await with List<T>.ForEach(Action<T>). YourBecause ForEeach only accepts Action<T> (void return type) your ForEach(async d => { ... } is giving ForEachbeing 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).

List<T>.ForEeach is the thing that does not support async/await. Your ForEach(async d => is giving ForEach a async void function. This makes ForEach not wait for the previous item to finish before moving to the next item. 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"); }; } 

You are not using async/await in a foreach loop, you are using async/await with List<T>.ForEach(Action<T>). Because ForEeach 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).

Source Link
Scott Chamberlain
  • 128k
  • 37
  • 299
  • 448

List<T>.ForEeach is the thing that does not support async/await. Your ForEach(async d => is giving ForEach a async void function. This makes ForEach not wait for the previous item to finish before moving to the next item. 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"); }; }