Middleware, DI, minimal APIs, config, auth, hosting
Practise these as flashcards. One or two lines each; the ones you miss come back first.
Start freeMiddleware are components that process HTTP requests and responses in a pipeline. The order matters because each middleware can modify the request or response, and some (like authentication) must run before others (like authorization) to work correctly.
You register a scoped service using services.AddScoped<IMyService, MyService>() in the DI container. This ensures one instance per HTTP request.
Minimal APIs let you define endpoints with less code, directly in Program.cs, which is great for small services. Controllers provide more structure, support for filters, and are better for larger or more complex applications.
You use the options pattern: create a POCO class, bind it in services.Configure<T>(...), and inject IOptions<T> where needed.
Transient services are created every time they're requested, scoped services are created once per HTTP request, and singleton services are created once for the application's lifetime. Choosing the right lifetime prevents issues like memory leaks or unintended shared state.
A captive dependency occurs when a singleton depends on a scoped or transient service. This causes the shorter-lived service to be held longer than intended, leading to bugs or memory leaks.
You call services.AddAuthentication().AddJwtBearer(...) in Program.cs or Startup.cs, specifying token validation parameters like issuer, audience, and signing key. Then, add the authentication and authorization middleware in the correct order.
[ApiController]?With [ApiController], model validation is automatic. If the model is invalid, the framework returns a 400 response with validation errors before the action runs.
You register health checks with services.AddHealthChecks() and map them with app.MapHealthChecks("/health"). This provides a standard endpoint for monitoring tools.
IOptions<T> interface?IOptions<T> provides access to configuration-bound settings in a type-safe way, allowing you to inject configuration sections as strongly-typed objects.
Forwarded headers like X-Forwarded-For and X-Forwarded-Proto let your app know the original client IP and protocol, which is important for logging, authentication, and generating correct URLs.
ASP.NET Core loads configuration files like appsettings.{Environment}.json based on the ASPNETCORE_ENVIRONMENT variable. You can override settings per environment and use secrets or environment variables for sensitive data.