ASP.NET Core Web API – Advanced Concepts

36. Global Exception Handling

Centralized error handling using middleware to avoid repetitive try-catch blocks.


app.UseExceptionHandler("/error");
app.Map("/error", () => Results.Problem("Something went wrong"));
    

37. Custom Middleware

Middleware used for request/response processing like logging or authentication.


public class RequestLoggingMiddleware
{
    private readonly RequestDelegate _next;

    public RequestLoggingMiddleware(RequestDelegate next) => _next = next;

    public async Task Invoke(HttpContext context)
    {
        Console.WriteLine(context.Request.Path);
        await _next(context);
    }
}
    

38. Rate Limiting

Controls number of API requests.


builder.Services.AddRateLimiter(options =>
{
    options.GlobalLimiter = PartitionedRateLimiter.Create(_ =>
        RateLimitPartition.GetFixedWindowLimiter("global", _ => new FixedWindowRateLimiterOptions
        {
            PermitLimit = 100,
            Window = TimeSpan.FromMinutes(1)
        }));
});
    

39. Kestrel Server

Default cross-platform web server in ASP.NET Core. Can run standalone or behind Nginx/IIS.


builder.WebHost.UseKestrel();
    

40. SignalR

Real-time communication (chat apps, live updates).


app.MapHub("/chat");
    

41. gRPC

High-performance communication using Protocol Buffers.


app.MapGrpcService();
    

42. Background Service

Runs long background tasks.


public class Worker : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            Console.WriteLine("Running...");
            await Task.Delay(10000);
        }
    }
}
    

43. Docker

Docker containerizes apps with dependencies for consistent deployment across environments.


FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
COPY . .
ENTRYPOINT ["dotnet", "MyApp.dll"]
    

44. Microservices

Docker containerizes apps with dependencies for consistent deployment across environments.

45. Clean Architecture

Layered architecture (API, Application, Domain, Infrastructure).

46. SOLID Principles

S → Single Responsibility
O → Open/Closed
L → Liskov Substitution
I → Interface Segregation
D → Dependency Inversion

47. Deadlock

Occurs when threads wait indefinitely for resources held by each other.


lock(obj1)
{
    lock(obj2)
    {
    }
}
    

48. Thread Pool

Pre-created threads reused to handle multiple requests efficiently


Task.Run(() => DoWork());
    

49. API Performance Optimization

  • Use async/await
  • Use caching
  • Optimize DB queries
  • Add pagination & indexing

50. API Security

  • Use HTTPS
  • JWT Authentication
  • Validation
  • Rate limiting & logging

[Authorize]
public IActionResult Secure() => Ok("Secure Data");
    

Advanced Quiz

Basic (1–15) Intermediate (16–35) Advance (36-50) Most Asking(51-100)