Say I have two scenarios:
1) WebApi Controller
[System.Web.Http.HttpPost] [System.Web.Http.AllowAnonymous] [Route("api/registerMobile")] public async Task<HttpResponseMessage> RegisterMobile(RegisterModel model) { var registerResponse = await AuthUtilities.RegisterUserAsync(model, _userService, User); if (registerResponse.Success) { var response = await _userService.GetAuthViewModelAsync(model.Username, User); return Request.CreateResponse(HttpStatusCode.OK, new ApiResponseDto() { Success = true, Data = response }); } else { return Request.CreateResponse(HttpStatusCode.OK, registerResponse); } }
2) MVC Controller
[Route("public")] public async Task<ActionResult> Public() { if (User.Identity.IsAuthenticated) { var model = await _userService.GetAuthViewModelAsync(User.Identity.Name); return View("~/Views/Home/Index.cshtml", model); } else { var model = await _userService.GetAuthViewModelAsync(null); return View("~/Views/Home/Index.cshtml", model); } }
I've been reading up on when I should use ConfigureAwait
and it seems like I should use ConfigureAwait(false)
on ALL of my async calls that are not tied directly to the UI. I don't know what that means though... should I be using .ConfigureAwait(false)
on all of the above await
calls?
I'm looking for some unambiguous guidelines around when exactly I should be using it.
This question is NOT the same as the Best practice to call ConfigureAwait for all server-side code - I am looking for a straightforward answer on the use-case for this method in the context of WebApi and MVC, not as general C#.
As a general rule, every piece of code that is not in a view model and/or that does not need to go back on the main thread should use ConfigureAwait false. This is simple, easy and can improve the performance of an application by freeing the UI thread for a little longer.
NET Core you won't need to spread ConfigureAwait(false) all over your code. Almost! This is almost true, it is still recommended the utilization of ConfigureAwait(false) for libraries as a fallback if those libraries are used within a legacy framework. But for most of the cases yes, in .
ConfigureAwait in Action You capture the current context before awaiting the task, leaving it to the task context, then recovering (re-entering) it back when the task completes.
ConfigureAwait(false), it's not a matter of preference. In UI contexts, you can't do that, because you may not return to the UI thread. Therefore this is a great answer as to why the default is the way it is.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With