Ad Code

✨🎆 JOIN MERN, JAVA, PYTHON, AI, DEVOPS, SALESFORCE Courses 🎆✨

Get 100% Placement Oriented Program CLICK to new more info click

Microservice Architecture in ASP.NET Core MVC

 

What is Microservice Architecture (Quick Practical Meaning)

Instead of one big application (Monolith), you create multiple small APIs, each responsible for one business function.

👉 Example (E-commerce):

  • User Service → Handles users
  • Product Service → Handles products
  • Order Service → Handles orders

Each runs independently.


🧱 Step-by-Step Practical Implementation in .NET Core

We’ll build a simple system:

👉 Services:

  1. User Service
  2. Product Service
  3. Order Service
  4. API Gateway

🧩 Step 1: Create Multiple Web API Projects

In Visual Studio / CLI:

dotnet new webapi -n UserService
dotnet new webapi -n ProductService
dotnet new webapi -n OrderService

👉 Each project = One microservice


🧩 Step 2: Design Each Service Independently

✅ Example: UserService

Model

public class User
{
public int Id { get; set; }
public string Name { get; set; }
}

Controller

[ApiController]
[Route("api/users")]
public class UsersController : ControllerBase
{
private static List<User> users = new List<User>()
{
new User { Id = 1, Name = "Shiva" }
};

[HttpGet]
public IActionResult GetUsers()
{
return Ok(users);
}
}

✅ Example: ProductService

[ApiController]
[Route("api/products")]
public class ProductsController : ControllerBase
{
[HttpGet]
public IActionResult GetProducts()
{
return Ok(new[] { "Laptop", "Mobile" });
}
}

✅ Example: OrderService (Calling Other Services)

This is where microservices become real 👇

[ApiController]
[Route("api/orders")]
public class OrdersController : ControllerBase
{
private readonly HttpClient _httpClient;

public OrdersController(HttpClient httpClient)
{
_httpClient = httpClient;
}

[HttpGet]
public async Task<IActionResult> GetOrders()
{
var users = await _httpClient.GetStringAsync("https://localhost:5001/api/users");
var products = await _httpClient.GetStringAsync("https://localhost:5002/api/products");

return Ok(new
{
OrderId = 1,
User = users,
Product = products
});
}
}

🧩 Step 3: Register HttpClient (IMPORTANT)

In Program.cs of OrderService:

builder.Services.AddHttpClient();

🧩 Step 4: Run Services on Different Ports

Example:

ServicePort
UserService5001
ProductService5002
OrderService5003

🧩 Step 5: Add API Gateway (Ocelot)

👉 Create Gateway Project:

dotnet new webapi -n ApiGateway

👉 Install Ocelot:

dotnet add package Ocelot

Configure ocelot.json

{
"Routes": [
{
"DownstreamPathTemplate": "/api/users",
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{ "Host": "localhost", "Port": 5001 }
],
"UpstreamPathTemplate": "/users",
"UpstreamHttpMethod": [ "GET" ]
}
],
"GlobalConfiguration": {
"BaseUrl": "https://localhost:7000"
}
}

Update Program.cs

builder.Configuration.AddJsonFile("ocelot.json");

builder.Services.AddOcelot();

var app = builder.Build();

await app.UseOcelot();
app.Run();

👉 Now instead of calling:

https://localhost:5001/api/users

You call:

https://localhost:7000/users

Method 1: Using launchSettings.json (Most Common)

📁 Path:

Properties/launchSettings.json

Example:

{
"profiles": {
"UserService": {
"commandName": "Project",
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}

👉 Change ports like this:

"applicationUrl": "https://localhost:6001;http://localhost:6000"

✅ Now your app runs on:


🔷 Method 2: From Program.cs (Code-Level Control)

Use Kestrel configuration:

var builder = WebApplication.CreateBuilder(args);

builder.WebHost.ConfigureKestrel(options =>
{
options.ListenAnyIP(5001); // HTTP
options.ListenAnyIP(5002, listenOptions =>
{
listenOptions.UseHttps(); // HTTPS
});
});

var app = builder.Build();
app.Run();



Post a Comment

0 Comments