IConfiguration vs IOptions NET
Synchronous and Asynchronous in .NET Core
Model Binding and Validation in ASP.NET Core
ControllerBase vs Controller in ASP.NET Core
ConfigureServices and Configure methods
IHostedService interface in .NET Core
ASP.NET Core request processing
| Backend for Frontend (BFF) Pattern | Saga Pattern in .NET Core | |
Service Discovery and Health Checks with .net core example |
Service Discovery is the process by which microservices automatically locate each other without hardcoding IP addresses or URLs.Itβs especially vital in dynamic environments where services scale up/down or move across hosts.
dotnet add package Consul
using Consul;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Hosting;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
var consulClient = new ConsulClient();
var registration = new AgentServiceRegistration()
{
ID = "my-service-id",
Name = "MyService",
Address = "localhost",
Port = 5000,
Check = new AgentServiceCheck()
{
HTTP = "http://localhost:5000/health",
Interval = TimeSpan.FromSeconds(10),
Timeout = TimeSpan.FromSeconds(5),
DeregisterCriticalServiceAfter = TimeSpan.FromMinutes(1)
}
};
await consulClient.Agent.ServiceRegister(registration);
app.MapGet("/health", () => Results.Ok("Healthy"));
app.MapGet("/", () => "Hello from MyService!");
app.Run();
using Consul;
var consulClient = new ConsulClient();
var services = await consulClient.Agent.Services();
foreach (var service in services.Response.Values)
{
if (service.Service.Equals("AnotherService"))
{
Console.WriteLine($"Found AnotherService at {service.Address}:{service.Port}");
}
}
/health)Health Checks are endpoints or probes that report the status of a service to help orchestrators manage traffic and recovery.They help orchestrators (like Kubernetes or Docker Swarm) decide whether a service is healthy and should receive traffic.
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
var builder = WebApplication.CreateBuilder(args);
// Register health checks
builder.Services.AddHealthChecks()
.AddCheck("self", () => HealthCheckResult.Healthy("Service is running"));
var app = builder.Build();
// Map health check endpoint
app.MapHealthChecks("/health");
app.MapGet("/", () => "Hello World!");
app.Run();
/health endpoint that returns HTTP 200 if healthyImagine a service registry like Consul:
This combo ensures resilience, scalability, and fault tolerance in distributed systems.
| Backend for Frontend (BFF) Pattern | Saga Pattern in .NET Core | |