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
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.
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.
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.
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:
and this SO question:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With