Calling ConfigureAwait from an ASP.NET MVC Action

前端 未结 3 1530
Happy的楠姐
Happy的楠姐 2021-02-19 01:45

I was working on a presentation and thought the following should fail since the ActionResult isn\'t being returned on the right context. I\'ve load tested it with VS and got no

3条回答
  •  闹比i
    闹比i (楼主)
    2021-02-19 02:23

    ASP.NET doesn't have the 'UI thread' need that many clients apps do (due to the UI framework below it). That context isn't about thread affinity, but for tracking the page progress (and other things, like carrying around the security context for the request)

    Stephen Toub mentions this in an MSDN article:

    Windows Forms isn't the only environment that provides a SynchronizationContext-derived class. ASP.NET also provides one, AspNetSynchronizationContext, though it's not public and is not meant for external consumption. Rather, it is used under the covers by ASP.NET to facilitate the asynchronous pages functionality in ASP.NET 2.0 (for more information, see msdn.microsoft.com/msdnmag/issues/05/10/WickedCode). This implementation allows ASP.NET to prevent page processing completion until all outstanding asynchronous invocations have been completed.

    A little more detail about the synchronization context is given in Stephen Cleary's article from last year.

    Figure 4 in particular shows that it doesn't have the 'specific thread' behavior of WinForms/WPF, but the whole thing is a great read.

    If multiple operations complete at once for the same application, AspNetSynchronizationContext will ensure that they execute one at a time. They may execute on any thread, but that thread will have the identity and culture of the original page.

提交回复
热议问题