using System.Threading.Tasks;
using System.Runtime.CompilerServices;
public static async Task Main()
await Task.Run(async () =>
Console.WriteLine("Async flow entered...");
if (Cache.Instance.Item.Value is {})
throw new InvalidOperationException("The async flow has just startet. A value should not be initialized.");
var newValue = new object();
Console.WriteLine($"Create: value = #{RuntimeHelpers.GetHashCode(newValue)}");
Cache.Instance.Item.Value = newValue;
Console.WriteLine("Async flow exitted.");
Console.WriteLine("Main finished.\n\n");
private static async Task Foo()
Console.WriteLine($"Foo: entered...");
Console.WriteLine($"Foo: getting value...");
var knownValue = Cache.Instance.Item.Value;
Console.WriteLine($"Foo: value = #{RuntimeHelpers.GetHashCode(knownValue)}");
Console.WriteLine($"Foo: exitted.");
public sealed class Cache
public static Cache Instance = new Cache();
public AsyncLocal<object> Item { get; } = new AsyncLocal<object>(OnValueChanged);
private static void OnValueChanged(AsyncLocalValueChangedArgs<object> args)
Console.WriteLine($"OnValueChanged! Prev: #{RuntimeHelpers.GetHashCode(args.PreviousValue)} Current: #{RuntimeHelpers.GetHashCode(args.CurrentValue)}");