using System.Collections.Generic;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
public static async Task Main()
await foreach (var v in Wrap(Sequence()))
static IAsyncEnumerable<int> Sequence()
static async IAsyncEnumerable<int> Core([EnumeratorCancellation] CancellationToken ct = default)
await Task.Delay(TimeSpan.FromSeconds(1), ct);
static IAsyncEnumerable<int> Wrap(IAsyncEnumerable<int> source)
static async IAsyncEnumerable<int> Core(IAsyncEnumerable<int> source, [EnumeratorCancellation] CancellationToken ct = default)
Test.Context.Value = "Hello world";
await foreach (var value in source.WithCancellation(ct))
var before = Test.Context.Value;
var after = Test.Context.Value;
Console.WriteLine($"Before={before}, after={after}");
public static readonly AsyncLocal<string> Context = new();