In this tutorial, we are going to discuss how to create a Microservice in Asp.Net Core 5.0 web application using Entity Framework Core and Docker. So, first-of-all, we will see “what is a microservice” And then we will see how to create a microservice in Asp.Net Core 5.0 application and then we will test our microservice using postman via IIS and then we will see how to run a microservice using docker.
What is a Microservice?
Microservice is an approach to create small services. Each service is running in their own space and can communicate via messaging. Each microservice works completely independently, but on the other hand, also all rely on each other. It means that each microservice has its own database.
Advantages of Microservices
There are below some advantages of Microservices:
- It is very easy to build and maintain.
- Smaller and faster deployment.
- Scalability.
- It is very easy to use different technologies.
- Ease of understanding.
Disadvantages of Microservices
There are below some disadvantages of Microservices:
- Communication between services is very complex: In case if your application consists of hundreds or thousands of microservices, then the application becomes really complex. Especially when the services talk to each other)
- More resources needed to use multiple services.
- Latency: Every call to another service adds some latency and the user has to wait longer until the result comes.
- Debugging is harder.
- Testing is harder.
- Cascading of problems to other services.
- Handling of the messages in the queue.
How to Build a Microservice using Asp.Net Core 5 and Docker
Let’s see how to build a microservice in Asp Net Core 5.0 using Entity Framework in step by step.
Step # 1: Create an Asp.Net Core 5 Application Solution
In this tutorial, we are going to create a solution for Asp.Net Core 5. So, go to visual studio 2019 and then click on the Create a new project and then click on the ASP.NET Core Web Application and then click on the Next button. Then enter the project name and then set the location for the project directory and then click on the Create button as you do see below in the screenshot.
Now, select the ASP.NET Core Web API project template and then click on the Create button as you do see below in the screenshot.
Then it will create a project as you do see below in the screenshot.
Step # 2: Create a Product Database
Now, in this step, we will create a database. So, first-of-all, we will create a Model and then we will see how to add an entity framework and then we will see how to create a database using migrations. So, follow these below steps.
Create Product Model:
So, go to project folder structure and then create a new folder with the name of Models as you do see below in the screenshot.
Now, right click on the Models folder and then Add and then select the Class… and then enter the name (E.g. Product.cs) and then click on the Add button as you do see in the below screenshot.
Now, add some properties in the Product.cs class as you do see below in the file.
Enable Entity Framework Core:
So, now we will enable entity framework core. First-of-all, we will install the Entity Framework Core Sql Server package. So, go to Tools and then NuGet Package Manager and then Manage NuGet Packages For Solutions… and search for Microsoft.EntityFrameworkCore and then install it as you do see below in the screenshot.
Now, search for Microsoft.EntityFrameworkCore.SqlServer and then install it as you do see below in the screenshot.
Now, add another package (Microsoft.EntityFrameworkCore.Tools) same as you have seen above.
Add Entity Framework Core DbContext:
So, first-of-all, we will add a new folder with the name of DataContexts as you do see below in the screenshot.
Now, right click on the DataContexts folder and then create a new class with the name of ProductContext and then write some code as you do see below in the file.
Note: – Don’t forget to add reference libraries.
Now, go to the project folder structure and then go to file appsettings.json and then add connections tring as you do see below in the file.
Now, go to the project folder structure and then open Startup.cs file and then add the service for Sql Server db as you do see below file’s code line # 31.
Note: – Don’t forget to add reference libraries.
Add Entity Framework Core Migrations
Now, in this step, we will add entity framework core migrations. So, go to Tools and then NuGet Package Manager and then Package Manager Console and then run the below command as you do see below in the screenshot. After completing successfully, then you will see a new folder with the name of Migrations.
Add-Migration InitialCommit
Note: – InitialCommit is the name of migration.
Now, run the below command then the migration will be applied to the current database.
Now, you will see the database will be created SQL Server Management Studio as you do see below in the screenshot.
Step # 3: Add Repository
Now, in this step, we will add a Repository to our project. So, go to project folder structure and then add a new folder with the name of Repository as you do see below in the screenshot.
Now, right click on the Repository folder and then add an interface with the name of IProductRepository interface and then write some code as you do see below in the file.
Now, right click on the Repository folder and then add a repository class with the name of ProductRepository.cs and then write some code as you do see below in the file.
Now, go to startup class and then add the below line of code within the ConfigureServices as you do see below in the screenshot.
services.AddTransient<IProductRepository, ProductRepository>();
Step # 4: Add Product Controller
In this step, we will add ProductController. So, go to project folder structure and then right click on the Controllers folder and then select Add and then select Controller… and then a new model will show. So, select API from the left pane and then select API Controller Controller with read/write actions and then click on the Add button as you do see below in the screenshot.
Now, add the name of the controller with the name of ProductController and then click on the Add button as you do see below in the screenshot.
Now, write the simple CRUD operations in the ProductController as you do see below in the file.
Run Product Microservice using IIS
Now, in this step we will test microservice using IIS. So, now press f5 or click on the IIS Express button as you do see in the below screenshot.
Now, we will test our Product Microservice using Postman. So, open the Postman and then test all the methods.
POST Method: So, first-of-all, we will test the POST method. So, go to postman and then select the method type POST from dropdown and then enter the URL (https://localhost:44326/api/Product) and then select Body and then select the raw radio button and then select JSON(application/json) and then add JSON and then click on the Send button as you do see below in the screenshot.
Then you will see the output as you do see below in the screenshot.
GET Method: Now, we will test the GET method. So, go to postman and then select the method type GET from dropdown and then enter the URL (https://localhost:44326/api/Product) and then click on the SEND button. Then you will see all the products as you do see below in the screenshot.
PUT Method: So, now, we will test the PUT method. So, go to postman and then select the method type PUT from dropdown and then enter the URL (https://localhost:44326/api/Product) and then select Body and then select the raw radio button and then select JSON(application/json) and then add JSON and then click on the Send button as you do see below in the screenshot.
Then you will see the updated data output as you do see below in the screenshot.
GET Product By ID: Now, we will test the GET method by ID. So, go to postman and then select the method type GET from dropdown and then enter the URL (https://localhost:44326/api/Product/1) and then click on the SEND button. Then you will see all the products as you do see below in the screenshot.
Delete Method: Now, we will test the Delete method. So, go to postman and then select the method type DELETE from dropdown and then enter the URL (https://localhost:44326/api/Product/1) and then click on the SEND button as you do see below in the screenshot. Then you will see the products will be deleted.
Run Product Microservice using Docker Container
As you know, we have enabled the Docker support while creating the project. So, now it’s very easy to run the service in the docker container. So, go to project folder structure and then right click on the project solution and then select Add and then select Container Orchestrator Support… as you do see below in the screenshot.
Now, select the container orchestrator from the dropdown and then click on the OK button as you do see below in the screenshot.
Now, you will see a Docker Support Options model. So, select Target OS and then click on the OK button as you do see below in the screenshot.
Then you will see docker-compose will be added to the project as you do see below in the screenshot.
When you save the solution then it will build the project under the container and create a docker image. You can see all the command execution in the output window
Now, go to the command prompt and then navigate to the folder where the project files are as you do see below in the screenshot.
Now, run the below command and then you will see all the created images.
docker images
Now, go to visual studio and then run the project with Docker as you do see below in the screenshot.
Now, run the below command then you will see all the running containers as you do see below in the screenshot.
As you have seen above the container is running on the port 32780:80
Now, run the service using the URL (http://localhost:32780/api/product) then you will see the exception as you do see below in the screenshot.
So, go to the appsettings.json file and then add the connection string as you do see below in the file.
Now, run the project and then you will see the output as you do see below in the screenshot.
Thank you for reading. Please keep visiting and sharing!
Leave a Reply