Synchronous Programming
In synchronous programming, tasks are executed one after the other, sequentially. When a request is made to a synchronous API, the server processes the request and waits for it to be completed before moving on to the next task. This means that if one operation takes a long time to finish, it can block the execution of subsequent operations, potentially leading to slower response times for clients.
Asynchronous Programming
On the other hand, asynchronous programming allows tasks to be executed concurrently. When a request is made to an asynchronous API, the server can initiate tasks and continue processing other requests without waiting for the previous tasks to be completed. This can lead to better scalability and responsiveness, especially in scenarios where certain operations, such as I/O operations, may take some time.
ASP.NET Core Web API
In ASP.NET Core Web API, you have the flexibility to choose between synchronous and asynchronous programming models. The framework supports both approaches. Asynchronous programming is particularly useful when dealing with I/O-bound operations, such as accessing a database or making external API calls, where the application can continue processing other tasks while waiting for the I/O operation to complete.
To implement asynchronous programming in ASP.NET Core Web API, you can use the `async` and `await` keywords in your controller methods, allowing you to write non-blocking code. This helps improve the overall performance and responsiveness of your API, especially in scenarios with high concurrency.
while synchronous programming is straightforward, asynchronous programming in ASP.NET Core Web API can enhance scalability and responsiveness by allowing the server to handle multiple tasks concurrently. The choice between synchronous and asynchronous programming depends on the specific requirements and characteristics of your application.
Let's Start the complete Implementation.
Create Model
namespace SyncVsAsyncProgrammingIn.NETCoreAPI.Model
{
public class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string Country { get; set; }
public string ZipCode { get; set; }
}
}
Application Db Context
using Microsoft.EntityFrameworkCore;
using SyncVsAsyncProgrammingIn.NETCoreAPI.Model;
using System.Collections.Generic;
namespace SyncVsAsyncProgrammingIn.NETCoreAPI.ApplicationDbContext
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}
public DbSet<User> Users { get; set; }
}
}
IUserRepository Interface For User Repository
using SyncVsAsyncProgrammingIn.NETCoreAPI.Model;
namespace SyncVsAsyncProgrammingIn.NETCoreAPI.IRepository
{
public interface IUserRepository
{
Task<User> GetUserByIdAsync(int id);
User GetUserByIdSync(int id);
}
}
User Repository
using Microsoft.EntityFrameworkCore;
using SyncVsAsyncProgrammingIn.NETCoreAPI.ApplicationDbContext;
using SyncVsAsyncProgrammingIn.NETCoreAPI.IRepository;
using SyncVsAsyncProgrammingIn.NETCoreAPI.Model;
namespace SyncVsAsyncProgrammingIn.NETCoreAPI.Repository
{
public class UserRepository : IUserRepository
{
private readonly AppDbContext _dbContext;
private readonly ILogger<UserRepository> _logger;
public UserRepository(AppDbContext dbContext, ILogger<UserRepository> logger)
{
_dbContext = dbContext;
_logger = logger;
}
public User GetUserByIdSync(int id)
{
try
{
// Synchronous database query
return _dbContext.Users.FirstOrDefault(u => u.Id == id);
}
catch(Exception ex)
{
_logger.LogError($"Error in UserRepository.GetUserById: {ex.Message}");
throw new ApplicationException("An error occurred while fetching user details. Please try again later.");
}
}
public async Task<User> GetUserByIdAsync(int id)
{
// Asynchronous database query
try
{
return await _dbContext.Users.FirstOrDefaultAsync(u => u.Id == id);
}
catch(Exception ex)
{
_logger.LogError($"Error in UserRepository.GetUserById: {ex.Message}");
throw new ApplicationException("An error occurred while fetching user details. Please try again later.");
}
}
}
}

Mariusz PostolPosted Jan 21, 2024, 4:58 PM
From the first sequence: "In synchronous programming, tasks are executed one after the other, sequentially. " we learn that programming and program execution are the same. In this sentence you refer also to the term "task" - what is the "task" in the language terms, I mean language syntax and semantics relevant for programming (program creation process)? In the next sentence, you refer to the "server" responsible for processing sth. My point is that a program is executed by a computer and it doesn't matter if it plays a server role. In other words, the server is a role but not an execution engine, isn't it? I am asking because I am working on a couple of articles addressing concurrent and parallel programming and looking for harmonization of terms.