Asynchronous call

When awaitencountered by the compiler, it converts the method asyncto a state machine, and the continuation is scheduled through

AsyncTaskMethodBuilder.AwaitUnsafeOnCompletedas indicated here or AsyncTaskMethodBuilder.AwaitOnCompletedas indicated.

View source .NET here , AsyncTaskMethodBuilder.AwaitUnsafeOnCompletedisAwaiter.UnSafeOnCompleted

It Awaiter.UnSafeOnCompleteddoesn't seem to pass an ExecutionContext (the code here ).

Please note that it runs from falseto flowExecutionContext. This means that if I use LogicalCallContext(part ExecutionContext) to store any data (e.g. activityId), then it will not flow in the continuation path, which means that I can not access it.

So my question is , what makes the compiler choose Unsafe Completion ?

Stephen Tube also mentioned the same thing here , but did not give any details. "All methods in the .NET Framework in which asynchronous operation of the fork processes and restores the ExecutionContext in this way (that is, all except those that have a prefix labeled" Unsafe "that are unsafe because they do not explicitly flow the ExecutionContext."

+4
source share
1 answer

, LogicalCallContext [...] [...],

. OnCompleted - . , .

, , Unsafe Completion?

, (.. , ICriticalNotifyCompletion).

AsyncMethodBuilderCore, , MoveNextRunner . , , .

, .

+2

Source: https://habr.com/ru/post/1684426/


All Articles