Is there any danger in using ConfigureAwait(false) in WebApi or MVC controllers?

前端 未结 4 1909
逝去的感伤
逝去的感伤 2021-01-31 07:43

Say I have two scenarios:

1) WebApi Controller

    [System.Web.Http.HttpPost]
    [System.Web.Http.AllowAnonymous]
    [Route(\"api/regi         


        
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-31 08:08

    it seems like I should use ConfigureAwait(false) on ALL of my async calls that are not tied directly to the UI.

    Not quite. That guideline doesn't make sense here, since there is no UI thread.

    The parameter passed to ConfigureAwait is continueOnCapturedContext, which explains more clearly the scenario. You want to use ConfigureAwait(false) whenever the rest of that async method does not depend on the current context.

    In ASP.NET 4.x, the "context" is the request context, which includes things like HttpContext.Current and culture. Also - and this is the undocumented part - a lot of the ASP.NET helper methods do depend on the request context.

    (Side note: ASP.NET Core no longer has a "context")

    should I be using .ConfigureAwait(false) on all of the above await calls?

    I haven't heard any firm guidance on this, but I suspect it's OK.

    In my own code, I never use ConfigureAwait(false) in my controller action methods, so that they complete already within the request context. It just seems more right to me.

提交回复
热议问题