In this article, we will create a microservice application that contains the product endpoint.
Step 2:
Step 3:
So creating a microservice means the application contains endpoints related to a specific module or component. So here I'm going to create 2 endpoints like HTTP GET, HTTP Post for the products to accomplish our demo. (We can implement the entire CRUD operation endpoint if we want).
Create A .NET6 Web API Application:
Let's create our first microservice application with .NET6 Web API application. Let's name our microservice project as 'Manufacture.Api'. To create .NET6 application recommended IDE's are Visual Studio 2022', 'Visual Studio Code'. In this demo, I'm using .NET CLI and Visual Studio Code editor.
CLI command
dotnet new webapi -o Your_Project_Name
dotnet new webapi -o Your_Project_Name
Install NuGet Packages:
Install EntityFramework Core NuGet.
.NET CLI command
dotnet add package Microsoft.EntityFrameworkCore --version 6.0.3
dotnet add package Microsoft.EntityFrameworkCore --version 6.0.3
Package Manager command
Install-Package Microsoft.EntityFrameworkCore -Version 6.0.3
Install-Package Microsoft.EntityFrameworkCore -Version 6.0.3
Install EntityFramework Core SqlServer NuGet.
Package Manager command
Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 6.0.3
Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 6.0.3
.NET CLI command
dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 6.0.3
dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 6.0.3
Setup Database Context:
Let's add an entity for our 'Products' table as 'Products.cs' file inside of the 'Data/Entities' folder.
Data/Entities/Products.cs:
namespace Manufacture.Api.Data.Entities; public class Products { public int Id { get; set; } public string Name { get; set; } public decimal Cost { get; set; } public string Description { get; set; } }Now add the Database Context class like 'ManufactureContext.cs' inside of the 'Data' folder.
Data/ManufactureContext.cs:
using Manufacture.Api.Data.Entities; using Microsoft.EntityFrameworkCore; namespace Manufacture.Api.Data; public class ManufactureContext: DbContext { public ManufactureContext(DbContextOptions<ManufactureContext> options) : base(options) { } public DbSet<Products> Products{get;set;} }
- (Line: 6) To make a class a database context class it must inherit the 'Microsoft.EntityFrameworkCore.DbContext'.
- (Line: 13) Register table class inside of the database context as a 'DbSet<TEntity>'.
appsettings.Development.json:
"ConnectionStrings": { "ManufactureDBConnection":"your_connection_string" }Now register our 'ManufactureContext' into the 'Program.cs' file.
Program.cs:
builder.Services.AddDbContext<ManufactureContext>(options => { options.UseSqlServer(builder.Configuration.GetConnectionString("ManufactureDBConnection")); });
Add ProductController:
Let's create our endpoint by adding 'ProductController'.
Controllers/ProductController.cs:
using Manufacture.Api.Data; using Microsoft.AspNetCore.Mvc; namespace Manufacture.Api.Controllers; [ApiController] [Route("[controller]")] public class ProductsController : ControllerBase { private readonly ManufactureContext _manufactureContext; public ProductsController(ManufactureContext manufactureContext) { _manufactureContext = manufactureContext; } }
- Here in our controller constructor we injected the 'ManufactureContext'.
Implement Create Operation:
Let's implement create operation endpoint. On creating an item in the response it is always a good idea to return the endpoint URL to get an item by its 'Id' value. So now we will create 2 action methods one for creating the item another to get an item by 'Id'.
Controllers/ProductControllers.cs:
[HttpGet] [Route("{id}")] public async Task<IActionResult> GetAsync(int id) { var product = await _manufactureContext.Products.FindAsync(id); return Ok(product); } [HttpPost] public async Task<IActionResult> PostAsync(Products newProduct) { _manufactureContext.Products.Add(newProduct); await _manufactureContext.SaveChangesAsync(); return CreatedAtAction("Get", new { id = newProduct.Id }, newProduct); }
- (Line: 1-7) The get action method to fetch the item by 'Id'.
- (Line: 2) Here in the 'Route' attribute added dynamic 'id' value its value will be passed to the action method input parameter.
- (Line: 5) Fetching the 'Product' by filtering it by 'Id'.
- (Line: 9-15) The post-action method to create a new item into the database.
- (Line: 12-13) Using 'ManufactureContext' to save the item into the database.
- (Line: 14) The 'CreatedAtAction' method generates the get endpoint and the endpoint will be returned as the header value of the response.
Step 2:
Step 3:
Implement Read Operation:
Let's implement a read operation to fetch all the products from the database.
Controllers/ProductsController.cs:
[HttpGet] public async Task<IActionResult> GetAsync() { var products = await _manufactureContext.Products.ToListAsync(); return Ok(products); }
- Here action method returns all 'Products' items as a response.
So that's about the 'Manufacture.API' microservice application that contains the 'Products' endpoint. In the next article, we are going to create our second microservice application like 'SalesBusiness.API' which consists of the 'Orders' endpoint.
Support Me!
Buy Me A Coffee
PayPal Me
Video Session:
Wrapping Up:
Hopefully, I think this article delivered some useful information Microservice flow in .NET6 applications. using I love to have your feedback, suggestions, and better techniques in the comment section below.
Refer:
Part-1 Introduction On Microservice And SQL Database Design For Microservices [.NET 6 MicorService Series]
Part-3 Create A Microservice For The Orders Endpoint[.NET6 Microservice Series]
Part-4 Asynchronous Data Communication Between Microservices Using RabbitMQ Message Broker With MassTransit[.NET6 Microservice Series]
Part-5 Ocelot API Gateway For Microservices[.NET6 Microservice Series]
Git Source
Part-3 Create A Microservice For The Orders Endpoint[.NET6 Microservice Series]
Part-4 Asynchronous Data Communication Between Microservices Using RabbitMQ Message Broker With MassTransit[.NET6 Microservice Series]
Part-5 Ocelot API Gateway For Microservices[.NET6 Microservice Series]
Git Source
Comments
Post a Comment