Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make AsyncLocal changes propagate to the calling function

DotNet Fiddle link https://dotnetfiddle.net/GqA32R

I have the following sample code to demonstrate the async local functionality

static AsyncLocal<string> _asyncLocalString = new AsyncLocal<string>();
    public static async Task Main()
    {
        // _asyncLocalString.Value = "Value 1";
        var t1 = AsyncMethodA();
        var t2 = AsyncMethodB();
        await Task.WhenAny(t1, t2).ConfigureAwait(false);
        Console.WriteLine("Finished");
        Console.WriteLine("Async local inside Main: " + _asyncLocalString.Value); // null
    }

    static async Task AsyncMethodA()
    {
        // _asyncLocalString.Value = "Value 2";
        Console.WriteLine("Method A");
        Console.WriteLine("Async local inside A: " + _asyncLocalString.Value); // null
        await Task.Delay(200);
    }

static async Task AsyncMethodB()
    {
        _asyncLocalString.Value = "Value 3";
        Console.WriteLine("Method B");
        await AsyncMethodC().ConfigureAwait(false);
    }

    static async Task AsyncMethodC()
    {
        await Task.Delay(100);
        Console.WriteLine("Method C");
        Console.WriteLine("Async local inside C: " + _asyncLocalString.Value); // Value 3 is printed
    }

Output:

Method B
Method C
Async local inside C: Value 3
Method A
Async local inside A: 
Finished
Async local inside Main: 

Is there a way to propogate these changes up the call stack so that async local changes in B are visible in A and main?

My real world scenario is similar to what I have above - the asyncLocal is set only in methodB and we have a few statements up the call stack which logs that value differently.

like image 220
user2133404 Avatar asked May 17 '26 15:05

user2133404


1 Answers

Is there a way to propogate these changes up the call stack so that async local changes in B are visible in A and main?

No, not with AsyncLocal<T>. async methods set their value context to "copy-on-write", so if it's written to, a copy will be created. And the copies never "flow" upwards.

You will need to use some other mechanism to achieve whatever it is you're trying to do.

like image 152
Stephen Cleary Avatar answered May 20 '26 07:05

Stephen Cleary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!