In this article, we will create two microservices using .NET Core 6 Web API and Containerization that services using Docker and connect them using Ocelot API Gateway.
Agenda
- Introduction of Microservice
- Introduction of Ocelot API Gateway
- Step-by-step implementation of microservices
- Containerization of Product and User Service using Docker
- Implementation of Ocelot API Gateway
Prerequisites:
- Understanding of .NET Core API, C#, and Docker
- Visual Studio 2022
- .NET Core 6 SDK
- Docker Desktop
- Postman
Introduction of Microservice
There are two types of architecture used for design applications: the first is monolithic, and the second one is microservices.
Monolithic Architecture
- In a monolithic Application, all components are tightly coupled to one another and run using a single process.
- It’s difficult to make changes in the future when monolithic architecture is used, because the components of the application will be tightly coupled to each other. This may impact all applications when trying to modify or introduce new things into the existing component.
- Suppose we have an E-commerce application, and in that application, we used multiple components, such as Login, Posts, and News Feed. In that case, when we introduce or change something in one of the components, it will impact the application because the application is tightly coupled and interconnected to each other using a single service.
- If we have deployed this type of application on the cloud, and in the future, we need to add something into the Posts Component, then we will be required to stop the whole application. This will impact performance and delivery services' efficiency. Also, it’s difficult to manage.
![]()
Microservice Architecture
- In the Microservice Architecture, we create application components that will work independently, without being tightly coupled to each other, in their own environments.
- Also, we can easily communicate with each other with the help of API Gateways and other such programs.
- So, basically, when we use microservices, they will run their own environment, and they will not impact one another when we change something in one of the services.
- Also, this is easy to manage while the application is in the development phase. Each developer will work independently on each service, and because of that, it is easy to manage and integrate components.
Introduction of Ocelot API Gateway
- Ocelot is the API Gateway for the .NET Platform and which is work as the entry point of our application when we use microservice architecture.
- The following diagram is from Microsoft Technical Documentation.
![]()
- API gateway is present between the client and microservices and acts as middleware for communication between different microservices as shown in the above diagram.
Step-by-step implementation of microservices
Let’s start.
Step 1
Create a Blank Empty solution using a visual studio.
![]()
Step 2
Configure your project.
![]()
Step 3
Project Structure:
![]()
Step 4
Create a new folder inside the solution and name it Microservices. Next, create ProductMicroservice and UserMicroservice projects inside that.
![]()
Step 5
Configure the ProductMicroservice project and path of service.
Step 6
Provide additional information related to the project.
![]()
Step 7
Next, create the UserMicroservice project inside the Microservices folder and follow the same steps as we used to create ProductMicroservice.
Step 8
Create an Ocelot API Gateway web API project outside the Microservices folder.
Let’s start the implementation of ProductMicroservice
Project Structure
![]()
Step 1
Install the following NuGet packages.
![]()
Step 2
Create Product Class inside the Model folder.
Step 3
Next, Create DbContextClass inside the Data folder.
Step 4
After that, create IProductService and ProductService inside the services folder.
IProductService
ProductService
Step 5
Configure the connection string inside the appsetting.json file.
Step 6
Later on, configure a few services inside the Program.cs class.
Step 7
Next, Create a ProductsController.cs.
Step 8
Finally, execute the following command in the package manager console to create migration and create the database.
Implementation of UserMicroservice service
Project Structure
![]()
Step 1
Install the following NuGet packages:
![]()
Step 2
Create a User Class inside the Model folder.
Step 3
Next, Create DbContextClass inside the Data folder.
Step 4
After that, create IUserService and UserService inside the services folder.
IUserService
UserService
Step 5
Configure the connection string inside the appsetting.json file.
Step 6
Later on, configure a few services inside Program.cs class.
Step 7
Next, create a UsersController.cs.
Step 8
Finally, execute the following command in the package manager console to create a migration, and create the database.
Containerization of Product and User Service using Docker
Step 1
Create a Docker file for ProductMicroservice inside the root folder.
Step 2
Next, create a Docker file for UserMicroservice inside the root folder.
Step 3
Create a docker-compose.yml file inside the main solution folder.
- Here you can see that we used the environment section to override the connection string that is present in the appsetting.json file
- Also, we put our machine IP Address over there in the connection string and port on which our SQL Server is running mode. Because if you put the direct server name, it will show some error while you are navigating your application which is run in a docker container.
- You can get your IP address using the ipconfig command through CMD.
Step 4
Open CMD inside the main solution folder where the docker-compose.yml file is present and execute the following command.
Step 5
Open Docker Desktop and you can see inside that there are two images created and is in running mode.
![]()
Step 6
Open the Container section and there you will see your two images running inside a container with their individual port number.
![]()
Step 7
Use the following URLs to check your microservices functionality which is running inside docker container.
Product Service
http://localhost:4201/swagger/index.html
![]()
User Service
http://localhost:4202/swagger/index.html
![]()
Implementation of Ocelot API Gateway
Step 1
Install the Ocelot NuGet Package.
![]()
Step 2
Project Structure:
![]()
Step 3
Create an ocelot.json file for routing.
- The Ocelot file has two sections: one is Global Configuration, which acts as an entry point of our application; the other is Routes, which is used to define routes of our microservices.
- Here we put the port of our services that are running inside the docker container.
- UpstreamPathTemplate is used to receive client requests and redirects them to the particular microservice.
- UpstreamHttpMethod is used to define HTTP attributes, and helps the gateway to get the type of request.
- DownstreamTemplatePath is the microservice endpoint that takes the request from UpstreamPathTemplate.
- DownstreamScheme defines the scheme of a request.
- DownstreamHostAndPorts defines the hostname and port number of microservices that are present inside the lauchSetting.json file.
Step 4
Configure the ocelot.json file inside Program.cs class and register services related to that.
Step 5
Right-click on the solution and open the property. Then, apply to start on API Gateway projects that we have created.
![]()
Step 6
After running your Ocelot API Gateway project, you will see this swagger UI:
![]()
Step 7
Open the Postman and check the functionality.
![]()
![]()
Conclusion
In this article, we discussed the working of microservice architecture and Ocelot API gateway using .NET Core 6 API and containerization of applications using Docker.
Happy coding!