Do you know about the ThreadLocal<T> type that allows you to declare data storage that's associated with a specific thread?

If you know that, do you know about a similar concept but for async methods? AsyncLocal<T> is the type that allows you to bind a value to a specific asynchronous method call.
If an async method continues on a different thread, ThreadLocal<T> would not work in such a scenario. That's why AsyncLocal<T> is used.

See the example to get more familiar with how it works!

Docs πŸ“‘: https://learn.microsoft.com/en-us/dotnet/api/system.threading.asynclocal-1?view=net-8.0

#dotnet #async #asynclocal #threadlocal
---
If you find this useful, consider giving a like & share β™»

AsyncLocal<T> Class (System.Threading)

Represents ambient data that is local to a given asynchronous control flow, such as an asynchronous method.