1. What is Async Programming?
Async means:
Do not block the thread
Let long tasks run in background
Free thread to serve other requests
.NET Core = High-performance, non-blocking, scalable framework.
Async is the core reason ASP.NET Core handles 10x more users.
2. Why We Use Async in ASP.NET Core?
2.1. To avoid blocking while doing I/O
Ex: database calls, web API calls, file handling.
2.2. To increase scalability
Same server handles more users.
2. 3. To keep application responsive
UI never freezes.
3. How Async Works Internally (Simple)
Sync Flow
Thread → Starts work → Waits → Returns result.
Async Flow
Thread → Starts work → Releases thread → Work continues in background → Returns.
4. Async Keywords
| Keyword | Meaning |
|---|
| async | Marks a method as asynchronous |
| await | Waits for the async task without blocking |
| Task | Represents an async operation (no return) |
| Task<T> | Async operation with return type |
| ValueTask | Lightweight task |
5. Basic Syntax
Async Method
public async Task<int> AddAsync(int a, int b)
{
await Task.Delay(1000); // example of async work
return a + b;
}
Calling Async Method
int result = await AddAsync(10, 20);
6. Real-Time Example 1: Database Call (EF Core)
Service Class
public class CustomerService
{
private readonly AppDbContext _context;
public CustomerService(AppDbContext context)
{
_context = context;
}
public async Task<List<Customer>> GetCustomersAsync()
{
return await _context.Customers.ToListAsync();
}
}
Controller
[HttpGet]
public async Task<IActionResult> GetAllCustomers()
{
var customers = await _customerService.GetCustomersAsync();
return Ok(customers);
}
ToListAsync() does not block any thread.
Server handles 1000 users smoothly.
7. Real-Time Example 2: SQL Query (ADO.NET) Async
public async Task<DataTable> GetOfflineOrdersAsync()
{
using (SqlConnection con = new SqlConnection(_connectionString))
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Orders", con))
{
await con.OpenAsync();
using (SqlDataReader dr = await cmd.ExecuteReaderAsync())
{
DataTable dt = new DataTable();
dt.Load(dr);
return dt;
}
}
}
8. Real-Time Example 3: Async API Call (HttpClient)
public async Task<string> CallApiAsync()
{
HttpClient client = new HttpClient();
string response = await client.GetStringAsync("https://example.com/api/data");
return response;
}
9. Real-Time Example 4: File Handling Async
public async Task WriteFileAsync()
{
string path = "sample.txt";
await File.WriteAllTextAsync(path, "Hello world!");
}
10. Example: Convert Sync MVC Method → Async MVC Method
Synchronous Controller
public IActionResult GetProducts()
{
var data = _service.GetProducts();
return Ok(data);
}
Asynchronous Controller
public async Task<IActionResult> GetProductsAsync()
{
var data = await _service.GetProductsAsync();
return Ok(data);
}
11. Example: Async Background Job (userdetails Example)
private async void userdetails _Tick(object sender, EventArgs e)
{
try
{
string time = Program.AppConfig["AppSettings:time"];
string query = "SELECT * FROM userdeatils";
using (SqlConnection con = new SqlConnection(SqlCon))
using (SqlCommand cmd = new SqlCommand(query, con))
{
await con.OpenAsync();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
// your logic
}
}
catch (Exception ex)
{
// log error
}
}
12. Rules When Using Async (Best Practices)
Do
Use async all the way
Use await on I/O operations
Use ToListAsync, FirstOrDefaultAsync
Use async in controller, service, repository
Don't
13. Full Real-World Async Architecture Pattern
Controller → Service → Repository → Database
async async async
Example
Controller
public async Task<IActionResult> GetData()
{
var data = await _service.GetDataAsync();
return Ok(data);
}
Service
public async Task<List<User>> GetDataAsync()
{
return await _repo.GetUsersAsync();
}
Repository
public async Task<List<User>> GetUsersAsync()
{
return await _context.Users.ToListAsync();
}
14. Diagram – How Async Frees the Thread
[User Request] → Thread 1 → Starts DB Query
↓
Thread returns to thread pool (free)
↓
DB finishes + returns data
↓
Thread 2 picks it up → Response sent
That’s why ASP.NET Core handles thousands of users.
15. Summary Table
| Concept | Meaning |
|---|
| async | method is asynchronous |
| await | wait without blocking |
| Task | async operation |
| Task | async operation with return |
| ToListAsync | EF Core async call |
| OpenAsync | SQL async |
| GetAsync | API async |
| WriteAsync | file async |