I've the following example
[TestMethod]
public void AsyncLocalFlowsInContinuation()
{
AsyncLocal<int> local = new AsyncLocal<int>();
var task1 = Task.Run(() =>
{
local.Value = 1;
});
local.Value.Should().Be(0);
var t1c = task1.ContinueWith((r) =>
{
local.Value.Should().Be(1); //THROW: it is 0 instead
});
local.Value.Should().Be(0);
t1c.Wait();
}
Isn't this supposed to work out of the box? in my understanding AsyncLocal should act like a static but specific for each task (in fact works for child task created inside "task1", for example) but instead it doesn't appear to have the same value in the continuation task "t1c". Should I specify something to allow the ExecutionContext to flow correctly inside the continuation task? or am i missing something obvuois?
i'm targetting .Net Standard 2.0 it is not mandatory to use AsyncLocal here, if it is the wrong type for this usage i'll be happy to use another one as long as the test's semantic will stay the same.