Singleton, scoped and transient lifetimes, captive dependencies, disposable services, scoped services inside singletons, IServiceScopeFactory, IServiceProvider and the service locator anti-pattern.
-
What is a captive dependency, and how does ValidateScopes help you catch it?
Junior
Theory
Service Lifetimes
-
Why does EF Core throw 'A second operation was started on this context instance', and what are the correct ways to run database work in parallel?
Mid-Level
Debugging
Service Lifetimes
-
Memory keeps growing in your API and a gcdump shows thousands of objects rooted by ServiceProviderEngineScope. What is happening and how do you fix it?
Mid-Level
Theory
Service Lifetimes
-
What are the singleton, scoped and transient lifetimes in .NET dependency injection? What exactly is a scope in an ASP.NET Core app, and what is it in a worker service where there is no HTTP request?
Covers the three built-in service lifetimes, how the container caches instances for each, what creates and disposes a scope in ASP.NET Core, and why worker services have no scope unless you create one.
Junior
Theory
Service Lifetimes
Β· π 3 follow-ups
-
How do you decide which lifetime to give a service? Walk through the questions you ask (state, thread safety, cost, dependencies), and say which lifetime you would choose for a DbContext, an HttpClient, an in-memory cache and a stateless domain service, and why.
Covers a decision process for choosing singleton, scoped or transient based on state, thread safety, construction cost and dependencies, with the standard choices for DbContext, HttpClient, caches and stateless services.
Intermediate
Theory
Service Lifetimes
Β· π 3 follow-ups
-
After a release, your API starts returning stale data for some users, and under load the logs show "A second operation was started on this context instance before a previous operation completed". You find a singleton PricingService that takes the scoped AppDbContext in its constructor. Explain what is happening, how you would confirm it, and how you fix it properly.
Covers diagnosing a captive dependency (scoped DbContext held by a singleton): symptoms, why Production does not catch it, confirming with ValidateScopes and tests, and fixing it by realigning lifetimes or creating scopes.
Mid-Level
Debugging
Service Lifetimes
Β· π 3 follow-ups Β· π 1 real-world
-
Who disposes the services the .NET container creates? Explain when singleton, scoped and transient disposables are disposed, what happens to a transient IDisposable resolved from the root provider, how IAsyncDisposable is handled, and in what order disposal happens.
Covers container ownership of IDisposable and IAsyncDisposable services: disposal per lifetime, root-tracked transient disposables, the sync-dispose-of-async-only-service exception, instances the container does not own, and reverse-order disposal.
Mid-Level
Theory
Service Lifetimes
Β· π 2 follow-ups Β· π 1 real-world
-
Your BackgroundService needs to read and update orders through the scoped AppDbContext and a scoped IOrderService. Show how you would write it correctly, explain why you can't inject them into the constructor, and cover cancellation, error handling and scope granularity.
Covers consuming scoped services from a singleton BackgroundService with IServiceScopeFactory.CreateAsyncScope per unit of work, including cancellation, per-item error handling, scope granularity and the .NET 8+ host behaviour on unhandled exceptions.
Mid-Level
Coding
Service Lifetimes
Β· π 3 follow-ups Β· π 1 real-world
-
What is the service locator anti-pattern? Why is injecting IServiceProvider and calling GetRequiredService inside your classes considered harmful, and where in an ASP.NET Core app is resolving from IServiceProvider acceptable?
Covers the service locator anti-pattern: hidden dependencies, runtime failures, testability and lifetime bugs, and the legitimate infrastructure uses of IServiceProvider such as composition roots, factories, middleware and workers.
Intermediate
Theory
Service Lifetimes
Β· π 3 follow-ups
-
Your ASP.NET Core service's memory grows steadily for days until the pod is OOM-killed, and a dotnet-gcdump shows thousands of instances retained through the DI container. Walk through how you diagnose DI-related memory growth (root-tracked transient disposables, long-lived scopes, captured singletons) and how you fix it.
Covers diagnosing memory growth caused by dependency injection: confirming managed growth with dotnet-counters, reading gcdump retention paths through ServiceProviderEngineScope, the three common DI causes, and fixes plus prevention.
Senior
Debugging
Service Lifetimes
Β· π 3 follow-ups Β· π 1 real-world
-
How do service lifetimes relate to thread safety? Explain what must be thread-safe in a singleton, why "A second operation was started on this context instance" happens even in scoped code, whether scoped services can be used from multiple threads in one request, and how per-request versus per-message scoping changes this.
Covers the concurrency contract each lifetime implies: thread-safe singletons, scoped services shared by parallel work within one request, DbContext concurrent use, and how message consumers and parallel processing need a scope per concurrent unit.
Senior
Theory
Service Lifetimes
Β· π 3 follow-ups Β· π 1 real-world
-
What does a DI scope mean outside an HTTP request? Explain how scopes work in message consumers, SignalR hubs, Blazor Server (and why scoped means per circuit there), Blazor WebAssembly, console apps and tests, and the bugs each one invites.
Covers who creates scopes and how long they last in message consumers, SignalR, Blazor Server circuits, Blazor WebAssembly, console apps and tests, with the typical bugs: long-lived DbContexts per circuit, missing scopes and root resolution.
Mid-Level
Theory
Service Lifetimes
Β· π 3 follow-ups Β· π 1 real-world
-
Code review: a Program.cs registers AppDbContext with AddSingleton, a TenantMiddleware takes the scoped ITenantContext in its constructor, a singleton ReportScheduler resolves a transient IDisposable exporter from an injected IServiceProvider on every run, and an HttpClient is registered as AddSingleton(new HttpClient()). What problems do you see, ranked by severity, and how would you fix them?
Covers reviewing DI registrations for lifetime defects: a singleton DbContext, a scoped service in a conventional middleware constructor, root-tracked transient disposables in a singleton, and a singleton HttpClient that never sees DNS changes, with the rewrite and review guard rails.
Senior
CodeReview
Service Lifetimes
Β· π 3 follow-ups Β· π 1 real-world