Search⌘ K
AI Features

Implementing an Asynchronous Controller Action Method

Explore how to implement asynchronous controller action methods in ASP.NET Core. Learn to transform synchronous endpoints into async ones using the async and await keywords along with asynchronous database calls. Understand the performance benefits and scalable design of async REST APIs through practical load testing and profiling.

Steps to implement an asynchronous controller action method

Now, we are going to change the implementation of the unanswered questions endpoint so that it's asynchronous:

  1. We are going to start by creating an asynchronous version of the data repository method that gets unanswered questions. So, let's create a new method in the data repository interface underneath GetUnansweredQuestions in IDataRepository.cs:

JavaScript (JSX)
public interface IDataRepository
{
...
IEnumerable<QuestionGetManyResponse>
GetUnansweredQuestions();
Task<IEnumerable<QuestionGetManyResponse>>
GetUnansweredQuestionsAsync();
...
}

The key difference with an asynchronous method is that it returns a Task of the type that will eventually be returned. ...