Total Page Preview:   000000000136

Why we use async in Asp.Net Core MVC?

Why we use async in  Asp.Net core MVC

In ASP.NET Core MVC, the term "async" refers to asynchronous programming, which is a way of executing code without blocking the main thread. Asynchronous programming is particularly useful in web applications where you want to avoid blocking the server's resources while waiting for external operations to complete, such as reading from a database, making an HTTP request, or performing other I/O-bound operations.
Here's an example to illustrate why and how you might use async in an ASP.NET Core MVC application:
Let's consider a simple controller action that fetches data from a database:
In the above example, the GetDataFromDatabase method simulates a database query by introducing a delay using Thread.Sleep(5000). This synchronous operation could potentially block the main thread, leading to poor application performance, especially in a high-traffic scenario.
Now, let's modify the code to use asynchronous programming:
In this modified example, the GetDataFromDatabaseAsync method is now marked as async and returns a Task<List<MyData>>. The asynchronous operation is simulated using Task.Delay(5000). By using async/await, the main thread is not blocked, allowing it to handle other requests while waiting for the asynchronous operation to complete.
In real-world scenarios, you would replace the simulated asynchronous operation with actual asynchronous database queries, HTTP requests, or other I/O-bound operations. Async programming in ASP.NET Core helps improve application responsiveness and scalability by allowing the server to handle more concurrent requests without tying up threads.

Thank You

About Author

Brijesh Kumar

Database Developer

I have more then 6 years Experience in Microsoft Technologies - SQL Server Database, ETL Azure Cloud - Azure SQL Database, CosmosDB, Azure Data Factory, PowerBI, Web Job, Azure Function, Azure Storage, Web Apps, Powershall and Database Migration On-Premise to Azure Cloud.
LinkedIn :





Facebook User: