AsyncLocal allows us to keep context data on a async control flow. This is pretty neat since all following resumes (even on another thread) can retrieve and modify the ambient data (AsyncLocal on MSDN).
Is there any way to 'leave' the current async local context for a sub-task and thus create a new one?
AsyncLocal<string> Data = new AsyncLocal<string>();
Data.Value = "One";
Task.Factory.StartNew( () =>
{
string InnerValue = Data.Value;
//InnerValue equals to "One", I need it to be null.
} );
In the example above, the inner task shares the AsyncLocal context with the outer control flow. Is there any way to enforce a new context?
Update: in order to solve my issue here, the following worked like a charm (despite the fact that it didn't entirely reset the context):
AsyncLocal<string> Data = new AsyncLocal<string>();
Data.Value = "One";
Task.Factory.StartNew( () =>
{
Data.Value = null;
string InnerValue = Data.Value;
//InnerValue equals to null now.
} );
string OuterValue = Data.Value; //Stays "one" even after the inner change.