Evening,
Can anyone point me to documentation for why the linq statement using an async and await operators will not propagate an exception up the stack but the foreach loop will? There has to be something under the hood and documented somewhere for this behavior, I am just having no luck finding it. #googlefoofail #doyouevenasyncbrah
If you run the unit test, you will see it come back with no errors and valid. But if you debug the unit test it will break on the error however it will allow you to continue and that error never propagates back up the stack.
If you comment out the linq statement and uncomment the foreach loop, that errors as expected when trying to add a duplicate key to the dictionary.
[TestMethod]
public void tester()
{
List<KeyValuePair<string, string>> pairs = new List<KeyValuePair<string, string>>()
{
{ new KeyValuePair<string, string>("test", "first") },
{ new KeyValuePair<string, string>("test", "second") }
};
fails = new Dictionary<string, string>();
pairs.ForEach(async x => await testasync(x)); //<========= does not throw error
//foreach (KeyValuePair<string,string> pair in pairs) //<========= throws error
//{
// testasync(pair).GetAwaiter().GetResult();
//}
Assert.IsTrue(1 == 1);
}
private Dictionary<string, string> fails;
private async Task testasync(KeyValuePair<string, string> pair)
{
fails.Add(pair.Key, pair.Value);
}