Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any benefit of having async methods in an asp.net web api controller?

As far as I know an asynchronous method call will start the method in a new thread and immediately give back control to the thread that initiate the call. So, in a WinForm application when there is a call to an asynchronous method inside an event handler then that method will be executed by a new thread and immediately the control will return back to the thread that initiate the call which in this case is the UI thread. At that point the UI thread is available to respond to other user requests until the asynchronous call is complete so the UI thread can resume the execution of the code inside the event handler.

My questions is what will happen in the same scenario but instead of a WinForms application we have and asp.net web api controller (or asp.net core controller which is for both web applications and web api’s) In WinForms the UI thread is the one that will re-gain control after calling an asynchronous method in order to be able to respond to other user interaction. What is the equivalent thread in asp.net web api (or asp.net core) and is there any other form of interaction that can respond to?

By the way, I’m not asking if it’s good or bad to use async methods in controllers. Some libraries offers only async methods so it’s inevitable. I’m just asking if ther is any benefit?

Thanks

like image 894
Dimitris Avatar asked Jan 12 '17 22:01

Dimitris


People also ask

Should Web API methods be async?

In the context of Web API, Asynchronous programming is used to improve the scalability of the application. By applying asynchronous programming using async and await there won't be any direct performance gain in speed instead application will be able to handle more concurrent requests.

Should I use async controller actions?

async actions help best when the actions does some I\O operations to DB or some network bound calls where the thread that processes the request will be stalled before it gets answer from the DB or network bound call which you just invoked.

What is the benefit of using async?

with async / await , you write less code and your code will be more maintainable than using the previous asynchronous programming methods such as using plain tasks. async / await is the newer replacement to BackgroundWorker , which has been used on windows forms desktop applications.


1 Answers

When it comes to IIS, .NET Framework maintains a pool of threads that are used to service ASP.NET requests. If a request is processed synchronously, the thread that processes the request is busy while the request is being processed, and that thread cannot service another request. Since the thread pool does not have an unlimited number of threads available at its disposal, we can run into problems with synchronous interfacing. Consequently, when you need to perform I/O intensive and other non-CPU bound requests, you will get the benefit by utilizing the asynchronous operations. You can use the AsyncController Class if you're using MVC versions prior to 4 and async methods if you are using MVC4 or later.

One good example of benefits of using async methods is if your web site is processing user submitted files. Depending on the file type, what kind of processing is being done and the size of the file this could be a resource intensive and time consuming operation so performing file operations using async methods would give you a clear benefit in that scenario.

Check out these articles:

  • Using an Asynchronous Controller in ASP.NET MVC
  • Using Asynchronous Methods in ASP.NET MVC 4

and this SO question:

  • Should I use AsyncController at ASP.NET MVC 4?
like image 170
Dean Kuga Avatar answered Oct 25 '22 00:10

Dean Kuga