public static void CaptureThenSuppressThenRunFlowTest()
{
ThreadTestHelpers.RunTestInBackgroundThread(() =>
{
var asyncLocal = new AsyncLocal<int>();
asyncLocal.Value = 1;
ExecutionContext executionContext = ExecutionContext.Capture();
ExecutionContext.SuppressFlow();
ExecutionContext.Run(
executionContext,
state =>
{
Assert.Equal(1, asyncLocal.Value);
VerifyExecutionContextFlow(asyncLocal, 1);
},
null);
Assert.Equal(1, asyncLocal.Value);
VerifyExecutionContextFlow(asyncLocal, 0);
ExecutionContext.RestoreFlow();
VerifyExecutionContextFlow(asyncLocal, 1);
executionContext = ExecutionContext.Capture();
asyncLocal.Value = 2;
ExecutionContext.SuppressFlow();
Assert.True(ExecutionContext.IsFlowSuppressed());
ExecutionContext.Run(
executionContext,
state =>
{
Assert.Equal(1, asyncLocal.Value);
VerifyExecutionContextFlow(asyncLocal, 1);
},
null);
Assert.Equal(2, asyncLocal.Value);
VerifyExecutionContextFlow(asyncLocal, 0);
ExecutionContext.RestoreFlow();
VerifyExecutionContextFlow(asyncLocal, 2);
});
}