使 AsyncLocal 更改传播到调用函数

Make AsyncLocal changes propagate to the calling function

点网 Fiddle link https://dotnetfiddle.net/GqA32R

我有以下示例代码来演示异步本地功能

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
    }

输出:

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

有没有办法将这些更改传播到调用堆栈,以便 B 中的异步本地更改在 A 和 main 中可见?

我的现实世界场景与我上面的场景类似 - asyncLocal 仅在 methodB 中设置,我们在调用堆栈上有一些语句以不同方式记录该值。

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?

不,AsyncLocal<T> 没有。 async methods set their value context to "copy-on-write",所以如果它被写入,将创建一个副本。而且副本永远不会 "flow" 向上。

您将需要使用一些其他机制来实现您想要做的任何事情。

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?

您可以用 AsyncLocal<StrongBox<T>> 包装实例。 这样,该值将存在于最初由父级设置的实例中。