using System.Threading.Tasks;
using System.Collections.Generic;
private static readonly AsyncBulkheadPolicy _throttlePolicy = Policy.BulkheadAsync(8, int.MaxValue);
private static readonly Random random = new Random();
public static async Task Main()
List<int> sources = Enumerable.Range(1, 10).ToList();
IEnumerable<Task<PolicyResult<int>>> projectTasks = null;
projectTasks = sources.Select(source => _throttlePolicy.ExecuteAndCaptureAsync(async () =>
await Task.Delay(TimeSpan.FromSeconds(0.01));
if (random.Next() % 2 == 1)
throw new Exception("TEST");
throw new AggregateException(new Exception("AGG TEST"));
await Task.WhenAll(projectTasks);
foreach (var task in projectTasks.Select(t => t.Result)
.Where(r => r.Outcome != OutcomeType.Successful)
if (r.FinalException is AggregateException)
return (r.FinalException as AggregateException).InnerExceptions;
Console.WriteLine(task.FinalException.Message);
Console.WriteLine("Hello World");