Aspnet Core Interview Questions Middleware Di 2025 Interview Questions & Answers
29 questions available
Mid
Answer
Pipeline execution flow:
Kestrel accepts connection
Request sent to IHttpApplication
Middleware executed in order (Use ? Map ? Run)
Endpoint routing middleware selects route
Filters (for MVC/Minimal API) run
Controller/Handler executes
Response travels backward through middleware pipeline
The pipeline is fully customizable.
Mid
Answer
Middleware:
Runs once per request, globally
Works at pipeline level
Can short-circuit requests
Filters (MVC/Minimal API):
Run after routing
Specific to controllers/actions
Ideal for validation, logging, caching
Mid
Answer
Use: Adds middleware and calls next()
Run: Terminal middleware; no next()
Map: Branches pipeline based on route/pattern
Mid
Answer
Because:
Authentication must run before Authorization
Exception handling must be at the top
Static files must run before MVC
CORS must run early
Changing order changes behavior drastically.
Mid
Answer
ASP.NET Core uses:
Constructor injection
Scoped container per request
Service descriptors
Activation via ActivatorUtilities
Disposable services cleaned at request end
A new service provider root is created during host startup.
Mid
Answer
To ensure:
Strong constructor contract
Testability
Immutable services
No hidden dependencies
Mid
Answer
Singleton: Same instance for entire application lifetime
Scoped: New instance per request
Transient: New instance on each resolution
Scoped services resolve inside request scope only.
Mid
Answer
Because:
Singletons live for entire app
Scoped services live only for request
Capturing scoped in singleton causes invalid references ? memory leaks, disposed objects
Mid
Answer
Minimal APIs:
Lightweight endpoint metadata
Use Endpoint Routing directly
No controllers, filters, attributes
Faster cold start
Better for microservices
MVC:
Full action pipeline
Filters, models, conventions
Mid
Answer
MVC:
Uses complex model binders
Handles form, route, query, headers, JSON
Validation attributes
Minimal APIs:
Source binding via attributes:
[FromRoute], [FromBody], [FromServices]
Faster & simpler binding
Mid
Answer
Endpoint Routing (ASP.NET Core 3+):
Routing happens early
Endpoints stored in EndpointDataSource
Middleware resolves endpoint before MVC executes.
Mid
Answer
Middleware can end pipeline early:
if (!auth) return;
Important for:
Performance
Security
Static file serving
Health checks.
Mid
Answer
Because exception handling middleware:
Is expensive
Logs errors unnecessarily
Slows down hot paths
Use Results or status codes instead.
Mid
Answer
Order of configuration providers:
appsettings.json
appsettings.{Environment}.json
User secrets (dev)
Environment variables
Command-line arguments
IConfigureOptions
Last writer wins.
Mid
Answer
IHostedService:
Start/stop lifecycle
Low-level control
BackgroundService:
Implements long-running ExecuteAsync()
Handles cancellation tokens
Ideal for queues, worker jobs.
Mid
Answer
Because:
No thread affinity
No deadlocks on async/await
Task scheduler handles async work
Higher throughput.
Mid
Answer
Use:
app.UseExceptionHandler("/error");
app.UseStatusCodePages();
Minimal API:
app.MapExceptionHandler();
Mid
Answer
ASP.NET Core does NOT use SynchronizationContext, so:
ConfigureAwait(false) does not change behavior
But still improves performance in library code.
Mid
Answer
Items: Lives for a single request
Session: Lives across requests, user-specific
Items ideal for correlation data.
Mid
Answer
Add custom model binders via:
services.AddControllers(options =>
options.ModelBinderProviders.Insert(0, new CustomBinder()));
This replaces or extends default binding for all controllers and actions.
Mid
Answer
It resolves culture using configured providers:
QueryStringRequestCultureProvider
CookieRequestCultureProvider
AcceptLanguageHeaderRequestCultureProvider
Then sets CultureInfo.CurrentCulture and CurrentUICulture before the rest of the pipeline.
Mid
Answer
Resource filters wrap the entire MVC pipeline.
They execute before model binding.
They can short-circuit expensive steps like:
Model validation
Action execution
Result execution
Useful for caching and authorization.
Mid
Answer
TypeActivatorCache precompiles constructors for:
Controllers
Middleware classes
View components
It avoids repeated reflection calls and improves object creation performance in heavy traffic apps.
Mid
Answer
Authorization policies define requirements.
Handlers evaluate those requirements per request.
Policies = rule definitions
Handlers = execution logic
Mid
Answer
CORS middleware validates browser-origin restrictions.
Reverse proxies (Nginx, Apache) do not enforce CORS; they only forward headers.
ASP.NET Core must use UseCors for browser-based protection.
Mid
Answer
DefaultHttpContext is a lightweight HttpContext implementation.
Used by Kestrel, Middleware, Testing, and object pooling.
Helps minimize allocations in high-load servers.
Mid
Answer
Enable buffering:
context.Request.EnableBuffering()
Copy the stream into memory, log it, then reset position.
For responses: wrap body in a MemoryStream.
Must avoid logging large payloads to prevent performance degradation.
Mid
Answer
Binding sources define where values come from:
Body, Route, Query, Header, Form, Services
Minimal APIs infer binding automatically but can be overridden with explicit attributes.
Mid
Answer
Use:
DiagnosticListener
Middleware timing wrappers
Application Insights RequestTelemetry
EventCounters for queue length, requests/sec, thread pool usage
Helps identify bottlenecks like slow database calls or blocking middleware.