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
| Reverse Proxy vs API Gateway :👈 | 👉:Production-Ready YARP Setup |
YARP Basics |
Microsoft created YARP (Yet Another Reverse Proxy).
YARP is built on ASP.NET Core.
Benefits:
YARP (Yet Another Reverse Proxy) is an open-source reverse proxy framework for .NET developed by Microsoft. It allows you to build a high-performance proxy server that receives client requests and forwards them to backend applications or services.
Instead of users accessing the backend applications directly, they connect to YARP, which decides where to send each request.
YARP can help with:
Suppose you have:
http://localhost:5001http://localhost:5002Using YARP, you can expose a single endpoint:
[http://localhost:8080/api/*](http://localhost:8080/api/*) [http://localhost:8080/admin/*](http://localhost:8080/admin/*)
YARP configuration:
{
"ReverseProxy": {
"Routes": {
"apiRoute": {
"ClusterId": "apiCluster",
"Match": {
"Path": "/api/{**catch-all}"
}
}
},
"Clusters": {
"apiCluster": {
"Destinations": {
"destination1": {
"Address": "[http://localhost:5001/](http://localhost:5001/)"
}
}
}
}
}
}
Install package:
dotnet add package Yarp.ReverseProxy
Program.cs:
var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddReverseProxy()
.LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));
var app = builder.Build();
app.MapReverseProxy();
app.Run();
Imagine you have:
http://localhost:7001http://localhost:7002Users access:
[https://company.com/customer](https://company.com/customer) [https://company.com/employee](https://company.com/employee)
YARP receives the requests and forwards them to the correct internal application. The users never see the internal server addresses.
Microsoft created YARP to provide:
dotnet new web
Install package:
dotnet add package Yarp.ReverseProxy
{
"ReverseProxy": {
"Routes": {
"route1": {
"ClusterId": "api-cluster",
"Match": {
"Path": "{**catch-all}"
}
}
},
"Clusters": {
"api-cluster": {
"Destinations": {
"destination1": {
"Address": "https://localhost:7000/"
}
}
}
}
}
}
var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddReverseProxy()
.LoadFromConfig(
builder.Configuration.GetSection("ReverseProxy"));
var app = builder.Build();
app.MapReverseProxy();
app.Run();
Proxy:
https://localhost:5000
Backend:
https://localhost:7000
Request:
https://localhost:5000/api/users
YARP forwards:
https://localhost:7000/api/users
Architecture:
Internet
|
v
YARP Gateway
|
+--> User API
|
+--> Product API
|
+--> Order API
Configuration:
{
"ReverseProxy": {
"Routes": {
"users": {
"ClusterId": "usersCluster",
"Match": {
"Path": "/users/{**catch-all}"
}
},
"products": {
"ClusterId": "productCluster",
"Match": {
"Path": "/products/{**catch-all}"
}
}
},
"Clusters": {
"usersCluster": {
"Destinations": {
"d1": {
"Address": "https://localhost:7001/"
}
}
},
"productCluster": {
"Destinations": {
"d1": {
"Address": "https://localhost:7002/"
}
}
}
}
}
}
A typical enterprise setup:
Each layer has a responsibility:
| Component | Responsibility |
|---|---|
| Front Door | Global routing |
| WAF | Security |
| YARP | Service routing |
| Services | Business logic |
Good:
User -> HTTPS -> Proxy -> HTTPS -> App
Avoid:
User -> HTTPS -> Proxy -> HTTP -> App
Especially across networks.
Without this, ASP.NET may see the proxy IP instead of the real client.
builder.Services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders =
ForwardedHeaders.XForwardedFor |
ForwardedHeaders.XForwardedProto;
});
app.UseForwardedHeaders();
builder.Services.AddHealthChecks();
Use proxy health probes.
Protect against abuse.
builder.Services.AddRateLimiter(...);
Log:
Useful for distributed systems.
Avoid single backend:
Proxy | +--App1 +--App2 +--App3
For Kubernetes or cloud-native apps:
Proxy -> Service Name -> Pods
instead of hardcoded IPs.
Bad:
Proxy -> Proxy -> Proxy -> Proxy
Verify backend URLs carefully.
Uploading:
2 GB file
may fail due to proxy limits.
Configure:
Then:
HttpContext.Connection.RemoteIpAddress
returns proxy IP instead of client IP.
Bad architecture:
Proxy | +-- App1 +-- App2
with in-memory sessions.
Use:
Bad:
Internet | One Proxy | Application
Use multiple proxy instances behind a load balancer.
| Reverse Proxy vs API Gateway :👈 | 👉:Production-Ready YARP Setup |