Calling ConfigureAwait from an ASP.NET MVC Action

前端 未结 3 1560
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条回答
  •  面向向阳花
    2021-02-19 02:38

    It appears because the Controller captures the Context whereas using System.Web.HttpContext is live access to what is part of the synchronization context.

    If we look at the ASP.NET MVC5 sources we can see that the ControllerBase class that all controllers inherit from has its own ControllerContext which is built from the RequestContext.

    I would assume this means that while the synchronization context is lost after a ConfigureAwait(false); the state of the Controller in which the continuation is happening still has access to the state of the control from before the continuation via the closure.

    Outside of the Controller we don't have access to this ControllerContext so we have to use the live System.Web.HttpContext which has all the caveats with ConfigureAwait(false);.

提交回复
热议问题