1

I recently began working in a new company. They have an ASP.NET Core 3.1 application hosted in IIS, which takes API calls from an angular app. They used async/await literally "everywhere": Methods in the ASP.NET controllers have signatures like public async Task<IActionResult> Foo(), all DB queries are made async, all calls to external APIs anyways (which I kind of understand).

So instead of simply

[HttpPost("api/ignorableDepartment")]
public IActionResult Add([FromBody] AddIgnorableDepartmentRequest model)
{
  return _ignorableGroupManager
          .Add(model)
          .ToResult();
}

the code now reads

[HttpPost("api/ignorableDepartment")]
public async Task<IActionResult> AddAsync([FromBody] AddIgnorableDepartmentRequest model)
{
    return await _ignorableGroupManager
                 .AddAsync(model)
                 .ToResultAsync()
                 .ConfigureAwait(false);
}

This application is only used by 1-2 people at the same time, and the calls in the method above would take 1ms at most.

In the past I would only use async/await if the processing is expected to last like 1 second or more, and in all other cases, I would just rely on IIS to take care of spreading multiple requests across threads/cores.

Did I just miss something in the past years, and this is the new way to develop? Or is that just overkill?

Updated information to answer questions in the comments

  • AddAsync adds the model to the database. So I guess it's ok
  • ToResultAsync() looks as follows (which makes no sense IMHO, since the controller method returns a Task<IActionResult> anyway. It also doesn't parse the passed information in any way, which a To... method would indicate)
public static async Task<IActionResult> ToResultAsync(this Task task)
{
    if (task == null)
    {
        throw new ArgumentNullException(nameof(task));
    }

    await task.ConfigureAwait(false);
    return new OkObjectResult(ResultResponse.Success);
}
4
  • What does _ignorableGroupManager.AddAsync(model) do? It's hard to tell what AddAsync does. If it's saving to db, then it's fine. by the way, async does not mean use a new thread. Commented Apr 13, 2021 at 11:00
  • You don't need ConfigureAwait(false). ASP.NET Core doesn't have a synchronization context that could be blocked Commented Apr 13, 2021 at 11:02
  • .AddAsync(model).ToResultAsync() I don't quite understand this notation. Methods ending with Async should return a Task and be awaited, not chain-called. Commented Apr 13, 2021 at 11:08
  • 2
    It's rather beneficial performance-wise to use async/await for IO-bound operations like working with a database or the file system. However, don't fall into the trap of doing a kind of fake async await Task.Run(() => //CPU-bound stuff) in the case of a server-side app. It can work well for UI apps like WPF cause the CPU-bound operation won't freeze it, but be careful when using that in the server-side app. Also, I think that it's not overkill, there's not so much overhead, just adding Task to signature and decorating it with async and finding async overloads of methods you use. Commented Apr 13, 2021 at 11:13

2 Answers 2

5

the calls in the method above would take 1ms at most.

In the past I would only use async/await if the processing is expected to last like 1 second or more

I think this is a misunderstanding here. "Asynchronous" does not mean "faster". Actually, because asynchronous requests have more bookkeeping work to do, asynchronous requests are actually (a tiny, tiny bit) slower.

Asynchrony in ASP.NET is all about scalability. Asynchronous requests free up more threads so that the server can handle more requests at the same time.

This application is only used by 1-2 people at the same time

So you're saying the application doesn't need to scale. Which is the primary benefit of asynchrony on ASP.NET.

Did I just miss something in the past years, and this is the new way to develop? Or is that just overkill?

There's a few different scenarios to consider.

One scenario is when you're writing new code. If you're writing new code (or a new app), then everything is asynchronous by default, and I'd argue that that's good. Perhaps the application will need to scale in the future. Or move to the cloud, where the pay-per-use model means asynchronous apps are often cheaper. Generally speaking, new apps should be asynchronous from the start.

Another scenario is when you have a synchronous app and you want to determine whether it's worth it to change it to be asynchronous. In this case, you have to evaluate the necessary scalability and future directions of the application, and make a judgement call on whether it's worth the developer time or not.

Sign up to request clarification or add additional context in comments.

4 Comments

"everything is asynchronous by default, and I'd argue that that's good", but what if the method is not doing anything asynchronous? I think the OP is asking that async/await is thrown all over the place even when there is no asynchronous work being carried out within the method.
@CodingYoshi: If there's no await, then there should be no async. But in both of the OP's examples, there was an await.
I meant if they are simply doing Task.FromResult on stuff but nothing asynchronous in actuality.
@CodingYoshi: Yes, in that case it should probably be synchronous.
-2

Yes this is the best practice of doing it.

I guess your colleagues are just used to do it this way, no matter how heavy the application is loaded.

It is a good practice! We are also doing it.

The point is if in some of the endpoints you want to do some relatively heavy operation (let's say more than 1-2 seconds), by using async/await you can free the thread to go and do some extra work instead of waiting. It is a performance gain in most cases,

So I recommend you to get used to it and start doing it.

1 Comment

why the dislikes ? which part of what I said is not correct ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.