0

When do I need ConfigureAwait(false) or when don't I ConfigureAwait(false)? Please tell me disadvantages of ConfigureAwait(false) also...

I have some concerns for ConfigureAwait(false) for each await async method like :

await UpdateDeliveryEventAsync(orderId, deliverystatus, _logger, new CancellationToken()).ConfigureAwait(false);

I hate it. We are developing middleware and we don't have any UI and our app is using as webhook by our clients means they are calling us. According to my experience, we don't have to use it because .NET Core 3.0 don't need ConfigureAwait(false) and found good explanation:

Q&A (origin of Stephen cleary answer)

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.

What do you think of using ConfigureAwait(false)? Do I need it in a middleware async application written by .net core?

Penguen
  • 16,836
  • 42
  • 130
  • 205
  • 2
    Unclear: Are you using ASP.Net Classic or are you using ASP.Net Core? If the former, you still have a context. If the latter then you don't so you don't need `ConfigureAwait`. But if you are building a library that you don't know where it may be used, you should use it regardless – Charlieface Jul 31 '22 at 16:00

0 Answers0