Search code examples
async-awaitasp.net-mvc-5.2configureawait

Is it really necessary to add .ConfigureAwait call when calling an awaitable method?


I am developing an application using Visual Studio 2019 with code validation.

Some code validation hints are important, however, I am getting a lot of hints in my awaitable method calls, such as

var appUser = await UserManager.FindByIdAsync(User.Identity.GetUserId());

In this case, system is suggesting me to put

var appUser = await UserManager.FindByIdAsync(User.Identity.GetUserId()).ConfigureAwait(true);

or

var appUser = await UserManager.FindByIdAsync(User.Identity.GetUserId()).ConfigureAwait(false);

Is it really necessary to dirty the code in such a way?

This is an MVC5 ASP.NET application.

Regards Jaime


Solution

  • Is it really necessary to dirty the code in such a way?

    If you do not block, then you do not need ConfigureAwait(false).

    In the general case, ConfigureAwait(false) is recommended because most code doesn't know whether its callers will block or not. But if this code is part of your application (i.e., not a library), and you're confident your application doesn't block on asynchronous code, then you're fine skipping the ConfigureAwait(false).

    Stephen Toub recently published a ConfigureAwait FAQ.