In this tutorial, we are going to learn how to implement an API Gateway using Ocelot. We will be looking at the below topics,
- What is an API Gateway?
- Introduction to Ocelot
- Create simple services
- Implement API Gateway using Ocelot
The source code can be downloaded from GitHub.
What is an API Gateway?
When building microservices based applications, an API Gateway is required to have a central place where authentication, throttling, orchestration, etc. is implemented.
Without an API Gateway in place, we might up end up implementing all these features (authentication, throttling, etc.) in each of the services and hence maintaining them for each service would be discouraging.
API Gateway acts as a security layer since we do not need to expose the microservices directly, instead, consumers requests will be reached at Gateway. Gateway breaks the requests and route them to the respective downstream microservices.
Introduction to Ocelot
Ocelot is an open-source API Gateway, designed for microservice architecture. This is based on .NET Core. Ocelot is a set of middleware designed for features such as routing, caching, security, rate limiting, etc. please refer to the link for more details.
Create Simple Services
We will be creating two simple services for demo purposes.
I have used the below tools for demonstration.
- VS 2022 Community Edition (64 bit)– Version 17.4.0 – Preview 2.0
- Web API
- .NET 6.0
- Ocelot
- Postman
Let us start two services – one is Student Service and another one is Teacher Service
Student Service looks like below
namespace StudentService.Controllers {
[Route("api/[controller]")]
[ApiController]
public class StudentController: ControllerBase {
private readonly IStudentRepository _studentRepository;
public StudentController(IStudentRepository studentRepository) => (_studentRepository) = (studentRepository);
[HttpGet]
public IActionResult GetAll() {
return Ok(_studentRepository.GetAll());
}
[HttpGet("{id}")]
public IActionResult Get(int id) {
var student = _studentRepository.Get(id);
if (student is null) return NotFound();
return Ok(student);
}
}
}
Update the launchSettings.json as below for Student Service,
"profiles": {
"StudentService": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
//"launchUrl": "swagger",
"applicationUrl": "http://localhost:5001",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
The entire source can be downloaded from GitHub.
Teacher Service looks like below,
namespace TeacherService.Controllers {
[Route("api/[controller]")]
[ApiController]
public class TeacherController: ControllerBase {
private readonly ITeacherRepository _repository;
public TeacherController(ITeacherRepository repository) => _repository = repository;
[HttpGet]
public IActionResult GetAll() {
return Ok(_repository.GetAll());
}
[HttpGet("{Id}")]
public IActionResult Get(int id) {
var teacher = _repository.Get(id);
if (teacher is null) return NotFound();
return Ok(teacher);
}
}
}
Update launchSettings.json for Teacher service as below,




Amita GuptaPosted Oct 19, 2023, 5:35 AM
Hi Prasad, now gateway is working fine on iis server. i had implement same in my other project and that project is using service discovery(consul) and gateway is not working for this. please help what i need to check? getting error 'localhost is currently unable to handle this request' http error 502. is there any platform where we can connect on call so that we can fixed that issue i can show you my code. we can connect on google meet. thanks in advance.
Amita GuptaPosted Oct 18, 2023, 1:40 PM
Hello Prasad, I have run your project and its working fine. i deployed on iis server, student service is working fine but when i am trying to run through gateway it doesn't work. can you please help me?
kavita pawarPosted Jun 19, 2023, 8:06 AM
Hi..We need to Create APi Gateway in MVC .net so can u plz share some source code
Mahesh SomiPosted Apr 24, 2023, 6:56 PM
Can you explain how can we use a custom middleware to encrypt the response