Bite-sized, standalone interview questions and answers for .NET. Read 5-10 per sitting. Each answer is 2-4 sentences max and stands on its own.

92 questions shown. Filter by keyword or difficulty below.

Topic: Minimal APIs & the Middleware Pipeline (Order 1)

It folds the _components list from last-to-first into one nested RequestDelegate. The last app.Use(...) call becomes the innermost function (closest to the terminal 404 delegate), and the first becomes the outermost — the first thing that runs on the way in and the last on the way out. There is no list being iterated at request time.

MapGet adds a RouteEndpoint to a separate EndpointDataSource, not to _components. It only runs if routing middleware (itself part of the Use-based pipeline) matches the request. Endpoint handlers are not part of the middleware chain and cannot intercept requests that never reach routing.

The entire rest of the pipeline — including all downstream middleware and the endpoint — is short-circuited silently. The client receives no error, no exception, and no response unless the middleware explicitly writes one. This is a common source of “endpoint never reached” bugs.

Two. Use() has no deduplication — each call appends another entry to _components, and Build() wraps each as its own nested layer. The second authentication middleware runs against the same HttpContext, producing redundant work and potential side effects.

It runs when the host starts processing requests, via WebApplication.BuildRequestDelegate(), after all app.Use(...) and app.Map...(...) calls in Program.cs have executed. The pipeline shape is fixed the moment Build() runs — nothing about the per-request path re-orders middleware.

Those are different failure modes with different fixes. A matched-but-uninvoked endpoint means routing found it but the pipeline never called it (typically a missing UseEndpoints wiring), while no match at all is a normal 404. Collapsing them would hide a wiring bug behind an ordinary response.

Topic: Dependency Injection Container Internals (Order 2)

A captive dependency is a scoped service captured by a singleton through constructor injection. The singleton holds the scoped instance for its entire lifetime, so every request that should get a fresh instance silently reuses a stale one — producing cross-request data leakage that never throws by default.

It walks each service’s ServiceCallSite tree at resolution time, tracking whether a scoped node appears while inside a singleton subtree. When a scoped service is found inside a singleton’s tree, it throws InvalidOperationException naming both services. The walk is memoized per Cache.Key so shared subtrees aren’t re-walked.

Injecting a scope factory into a singleton is the sanctioned pattern for consuming scoped services on demand — the singleton creates short-lived scopes via CreateScope() instead of capturing a single scoped instance. The validator permits this escape hatch while still catching direct constructor capture.

ValidateScopes checks services only when they are actually resolved at runtime — a captive dependency in a rarely-used singleton could go undetected until that code path executes. ValidateOnBuild eagerly validates every registered service at BuildServiceProvider() time, closing that gap.

No. A transient service is constructed fresh on every injection, so the singleton always gets a new instance at construction time. There’s no cross-request state sharing. CallSiteValidator only flags scoped services because only scoped services carry the “should be fresh per scope” guarantee a singleton violates.

The CallSiteValidator tree-walk is built around constructor-injection call-site analysis. A factory delegate (AddSingleton<T>(sp => ...)) that manually resolves a scoped service from the passed IServiceProvider bypasses this analysis and can reintroduce the same captive-dependency bug without detection.

Topic: Configuration & the Options Pattern (Order 3)