'await' 运行 之后可以在 ASP.NET 的不同线程中编码吗?

Can code after 'await' run in different thread in ASP.NET?

我在此处 https://blog.stephencleary.com/2009/10/synchronizationcontext-properties.html 了解到 ASP.NET 应用程序的执行上下文没有特定的关联线程。这是否意味着 await 之后的代码将(可以)在具有相同上下文的不同线程中执行?在这种情况下,同步执行怎么可能导致死锁呢?或者ASP.NET应用程序不是这种死锁的情况?

提前致谢。

对于ASP.NETClassic(.NET Framework),有一个特殊的AspNetSynchronizationContext,续集会post回到原来的上下文线程。

ASP.NET核心没有。如果您检查 SynchronizationContext.Current,您会发现它设置为 null。因此,continuation 可以自由使用它选择的任何线程,并且在这方面不会遇到经典死锁


更新

评论中 @StephenCleary 的一些重要修正

Minor correction : on classic ASP.NET, the SynchronizationContext represents the request context, not a specific thread.

The method may resume on any thread pool thread after the await. The deadlock occurs because there is a lock as part of that request context to ensure that only one thread at a time may be in the request context.

So, when the async method is ready to resume, a thread pool thread is taken which enters the request context and tries to take that lock. If there's another thread blocked on that task in the context, the lock is already taken and a deadlock will occur