0

I am using async/await in MVC, but only when I have more than one task (WaitAll).

I understand that having only one task is good to have the UI free, in case of WPF or Windows Form, but does it make sense for MVC to have only one task and await for that?

I've seen it a lot in code, in MVC, but I don't get the advantages.

5
  • 4
    WaitAll is blocking method, it isn't a good idea to use with the asynchronous code Commented Mar 9, 2020 at 18:54
  • 1
    "I understand that having only one task is good to have the UI free" - where did you hear that? Commented Mar 9, 2020 at 18:55
  • 1
    You should read up on async await. It's a leaky abstraction, and you need to know how it works under the hood to use it successfully. Commented Mar 9, 2020 at 18:56
  • 1
    Start here and read all of the articles in that section. Then read Stephen Cleary's articles on Async/Await. Commented Mar 9, 2020 at 19:00
  • 1
    If you are running a web application, you are running a very multi-threaded application; each ASP.NET request gets serviced by a different thread pool thread. What await does is allow you to step off your thread of execution and do some work. In a web app, you should really only be awaiting I/O operations, but, async and await allow you to do those operations away from the request's thread. Commented Mar 9, 2020 at 19:01

3 Answers 3

2

HTTP requests are handled by thread pool threads.

If you block a thread, it will not be able to do other work. Because the total number of threads is limited, this can led to the starvation of the thread pool and new requests will be denied - 503.

Using async code, the thread is released back to the thread pool, becoming available to handle new requests or the continuations of async code.

Like on client UIs, on the server, async code is all about responsiveness, not performance. Requests will take longer but your server will be able to handle more requests.

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

4 Comments

Is this a "YES" (always use async Task<ActionResult>)?
NO. Only if you something async to invoke. Don't use Task.Run, becaue it will only use more thread pool threads.
Thanks, Paulo. But what if all I have in an action is a simple LINQ to SQL. Should I use ToListAsync() instead of ToList() and make the action Task<ActionResult>?
Yes, because instead of being blocked waiting for the database to answer, the thread can be used to handle another request.
0

It depends on what you are trying to achieve. For instance, if you have multiple calls to multiple services you can always do it in a way that only the last call makes the rest of the system "wait".

You can optimise your code in a way that X asynchronously calls to services start (almost) at the same time without having to 'await' for one another.

 public async Task RunSomethings()
    {
        var something1 = AsyncCall1();
        var something2 = AsyncCall2();
        var something3 = await AsyncCall3();
    }

    private async Task<Something1> AsyncCall1()
    {
        return await Something1();
    }

    private async Task<Something2> AsyncCall2()
    {
        return await Something2();
    }

    private async Task<Something3> AsyncCall3()
    {
        return await Something3();
    }

I hope it helps.

1 Comment

it does, thanks. But what if I have only one service. I call to an Action which calls to a single service. Is it reccomended to make all async (the service & the action in the controller)? - basically this is what I want to find out. What If I have to tasks, one depending on the other, is it wise to await one and then await the other?
0

Good question. Using asynchronous methods is all about using the resources effectively as well as give a good user experience. Any time you need to call on a resource that could take time to collect, it's good to use an async call.

This will do a few things. First, while a worker thread is waiting for data, it can be put on 'hold' so to speak, and that worker thread can do something else until the data is returned, an error is returned or the call just times out.

This will give you the second advantage. The interface the user is using to call the resource will be released, temporarily, to do other things. And overall, less server resources are consumed by idle processes.

I'd recommend watching the videos on this page here: https://channel9.msdn.com/Series/Three-Essential-Tips-for-Async

It's probably the clearest explanation that can help leapfrog your learning on async.

5 Comments

Can you explain this? "This will give you the second advantage. The interface the user is using to call the resource will be released, temporarily, to do other things. And overall, less server resources are consumed by idle processes."
Think of going to a bank. There are three tellers available. There are 50 people in line to see a teller. If the teller has to handle all processes, such as processes that require a manager signature, for example, it will cause the line to move very slowly. But if the teller could dispatch the customer to a secondary representative, they can take the next customer. Once the representative gets the manager signature, the teller could then bring that person back to them once they finish with their current customer. This in a simplified version is what's happening.
That would apply if this was a client UI (Windows Forms, WPF) application. If it's ASP.NET, your talking with your bank's website, not your bank teller. You have no way of knowing what they are doing and how over there.
So for MVC it's not worth it. Right? Because the UI doesn't freeze as on WPF/Windows Forms
Actually for MVC, it becomes more important. There is a finite amount of resources assigned by a server for processing calls. Back to the teller story above. Using async properly will allow you to keep all threads assigned at different layers busy with tasks and not having tasks that are waiting for other things to complete. Once resources become exhausted, then new web requests have to wait for their turn, like standing in line at the bank.

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.