Making API Controllers Asynchronous
Explore how to convert synchronous API controllers to asynchronous methods in ASP.NET Core to enhance scalability. Understand the difference between synchronous and asynchronous I/O calls, learn to profile performance using Visual Studio, and see how async code impacts server thread management and request handling.
We'll cover the following...
In this section, we are going to make the unanswered questions endpoint asynchronous to make it more scalable.
At the moment, all of our API code has been synchronous. For synchronous API code, when a request is made to the API, a thread from the thread pool will handle the request. If the code makes an I/O call (such as a database call) synchronously, the thread will block until the I/O call has finished. The blocked thread can't be used for any other work—it simply does nothing and waits for the I/O task to finish. If other requests are made to our API while the other thread is blocked, different threads in the thread pool will be used for the other requests. The following diagram is a visualization of synchronous requests in ASP.NET: