I am examining some ASP.NET Web API code that is using .NET 4.7.2.
Here is an example controller and action method:
public class ThingController : System.Web.Http.ApiController
{
// ...
public async Task<IHttpActionResult> GetValue()
{
var value = await _db.GetValue().ConfigureAwait(false);
return Content(value);
}
}
I have read that best practice is to not use ConfigureAwait
in application code so that execution continues with the captured synchronization context, since there may be needed state associated with the captured context. However, in general we should use ConfigureAwait(false)
so that we don't continue on the captured synchronization context unnecessarily.
So my thoughts are that we don't want to be calling ConfigureAwait(false)
anywhere in this Web API code.
Then I read about deadlocks and that it doesn't matter when using ASP.NET Core (although I'm not).
I've added a breakpoint and checked SynchronizationContext.Current
which is null
.
Can I safely remove all calls to ConfigureAwait(false)
from this project? If not, in which cases should these calls remain?