public static async Task CaptureAndRestore()
{
AsyncLocal<int> local = new AsyncLocal<int>();
local.Value = 42;
ExecutionContext ec = ExecutionContext.Capture();
local.Value = 12;
ExecutionContext.Run(
ec,
_ =>
{
Assert.Equal(local.Value, 42);
local.Value = 56;
},
null);
Assert.Equal(local.Value, 12);
}