AggregateException C# example

后端 未结 5 431
我寻月下人不归
我寻月下人不归 2020-12-15 16:51

I have seen an example of AggregateException on the web and I\'m trying to figure out how it works. I have written a simple example, but my code for some reason

5条回答
  •  伪装坚强ぢ
    2020-12-15 17:11

    My way to resolve it:

    var tasks = new Task[] { DoSomethingAsync(), DoSomethingElseAsync() };
    
    try
    {
        await Task.WhenAll(tasks).ConfigureAwait(false);
    }
    catch (AggregateException ex)
    {
        var flatAgrExs = ex.Flatten().InnerExceptions;
    
        foreach(var agrEx in flatAgrExs)
        {
            //handle out errors
            logger.LogError(agrEx, "Critical Error occurred");
        }
    }
    

提交回复
热议问题