newbedev.com Open in urlscan Pro
2606:4700:3033::6815:5d83  Public Scan

URL: https://newbedev.com/why-does-asynclocal-t-return-different-results-when-code-is-refactored-slightly
Submission: On November 24 via manual from PL — Scanned from DE

Form analysis 0 forms found in the DOM

Text Content

Menu
NEWBEDEVPythonJavascriptLinuxCheat sheet
Contact

NEWBEDEV
 * Python
 * Javascript
 * Linux
 * Cheat sheet
 * Contact
 * 




WHY DOES ASYNCLOCAL<T> RETURN DIFFERENT RESULTS WHEN CODE IS REFACTORED
SLIGHTLY?

Follow this link AsyncLocal Class on MSDN

> AsyncLocal<T> represents ambient data that is local to a given asynchronous
> control flow, such as an asynchronous method

It means that your code uses different values when it's accesses from another
async method such as WrapperAsync and your main thread contains another value

00:10 / 00:20

T-Mobile G2 Unboxing


[UPDATE]
Not obvious thing to understand, but here is explanation. Control Flow in Async
Programs. This is how your thread is changed when you do not expect this.


This is how Control Flow working with async

public class Program
{
    private static readonly AsyncLocal<string> AsyncLocalContext = new AsyncLocal<string>();

    public static void Main(string[] args)
    {
        AsyncLocalContext.Value = "No surprise";
        WrapperAsync("surprise!");
        Console.WriteLine("Main: " + AsyncLocalContext.Value);
    }

    private static async void WrapperAsync(string text)
    {
        Console.WriteLine("WrapperAsync before: " + AsyncLocalContext.Value);
        AsyncLocalContext.Value = text;
        Console.WriteLine("WrapperAsync after: " + AsyncLocalContext.Value);
    }
}


Output is:

WrapperAsync before: No surprise
WrapperAsync after: surprise!
Main: No surprise


[/UPDATE]


AsyncLocal<T> is ambient data stored on the ExecutionContext of the current
thread. ExecutionContext is flowed across threads automagically in async/await
call chains (see Stephen Toub's blog for details). When the app starts, the
default ExecutionContext is used, but once data is stored via
AsyncLocal<T>.Value, a new ExecutionContext is created for the current async
call chain (see here) and the ambient data is added to it. This new context is
propagated to downstream calls.

Stephen Cleary discusses this behavior here (scroll down to the AsyncLocal
section) and makes the point:

> [AsyncLocal] provides a way for contextual information to flow “down”
> asynchronous calls. Note that the value does not flow “up”.

This is why AsyncLocal<T> updates down the call chain are not reflected in
upstream methods.


TAGS:

C#

.NET

ASYNC AWAIT


RELATED

C# is the Main problemLeibniz golf in C#Code Injection works in C# too!Shortest
Hello World program with no semi colonsUpdate .NET website without reloadingCan
CLR execution continue after 'THROW' is encountered in T-SQL?Setting up a
central CLR stored procedure / function respository library for internal stored
procs in other databases to use?Daily database maintenance using SQL Server 2008
and a stored procedure3rd party dll in SQL Server CLRLogin to SQL Server using
Windows Authentication


RECENT POSTS

Pandas how to find column contains a certain valueRecommended way to install
multiple Python versions on Ubuntu 20.04Build super fast web scraper with Python
x100 than BeautifulSoup


© 2021 newbedevPrivacy Policy