In short - yes.
SynchronizationContext
is not captured.
LogicalCallContext
is cleared.
_
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
namespace Tests
{
[TestFixture]
public class ExecutionContextFlowTest
{
[Test]
public async Task Execute()
{
try
{
ThreadPool.SetMinThreads(2, 100);
ThreadPool.SetMaxThreads(2, 100);
var asyncLocal = new AsyncLocal<bool>();
await Task.Run(
() =>
{
asyncLocal.Value = true;
CallContext.LogicalSetData("test", true);
});
await Task.WhenAll(
Enumerable.Range(0, 10).Select(
_ => Task.Run(
() =>
{
Assert.That(asyncLocal.Value, Is.False);
Assert.That(CallContext.LogicalGetData("test"), Is.Null);
})).ToArray());
}
finally
{
ThreadPool.SetMinThreads(10, 100);
ThreadPool.SetMaxThreads(100, 100);
}
}
}
}
Though I'm not sure about SecurityContext
.
[ThreadStatic]
and ThreadLocal
are not controlled by ExecutionContext
(and not cleared).
Also in .NET sources I found a cleanup which replaces ExecutionContext
with previous (empty) ones after item execution is completed.