The built-in .NET dependency injection container in depth: registration and resolution, service lifetimes and their failure modes, and advanced techniques such as keyed services, decorators, open generics and graph validation.
-
What is the composition root, and why should only the composition root resolve services from the container?
Beginner
Theory
DI Core Concepts
-
Why is calling services.BuildServiceProvider() inside Program.cs a problem, and what do you do instead?
Junior
Theory
DI Core Concepts
-
What does ValidateOnBuild check, and what can it not catch?
Intermediate
Theory
DI Core Concepts
-
What is the difference between inversion of control, dependency injection and the Dependency Inversion Principle? Why do we use DI at all, and what does it look like in an ASP.NET Core app?
Defines inversion of control, dependency injection and the Dependency Inversion Principle, how they relate, why DI is used, and how the built-in ASP.NET Core container applies it.
Beginner
Theory
DI Core Concepts
Β· π 3 follow-ups
-
How do you register services with the built-in .NET container? Explain AddSingleton, AddScoped and AddTransient, the TryAdd* methods and Replace, and what happens when you register several implementations of the same interface and inject IEnumerable<T>.
Covers registering services in IServiceCollection: lifetime methods, TryAdd, TryAddEnumerable and Replace, last-registration-wins resolution and injecting all implementations with IEnumerable<T>.
Junior
Theory
DI Core Concepts
Β· π 3 follow-ups
-
How does the built-in .NET container actually resolve a service? Explain how it chooses a constructor, what call sites are, how ActivatorUtilities differs from GetService, and how the container makes resolution fast after the first call.
Explains resolution in Microsoft.Extensions.DependencyInjection: constructor selection rules, the call-site tree and cache, runtime and compiled resolvers, ActivatorUtilities vs GetService, and what errors you get when resolution fails.
Intermediate
Theory
DI Core Concepts
Β· π 3 follow-ups
-
What are constructor injection, property injection and method injection? Why does the built-in .NET container only support constructor injection, and where do you still see method injection in ASP.NET Core?
Compares constructor, property and method injection, explains why Microsoft.Extensions.DependencyInjection supports only constructors, and shows where ASP.NET Core injects into methods and properties (endpoint parameters, [FromServices], InvokeAsync, @inject, [Inject]).
Junior
Theory
DI Core Concepts
Β· π 3 follow-ups
-
Sometimes a service needs an object it can't get from the container at construction time: a dependency that is expensive and rarely used, a new instance per operation, or one that needs a run-time value such as a tenant or file name. Show how you would solve this with factory delegates, Func<T>, Lazy<T> and ActivatorUtilities, and explain the trade-offs.
Shows factory injection in Microsoft.Extensions.DependencyInjection: factory registrations, explicit Func<T> and Lazy<T> registrations, typed factory interfaces, ActivatorUtilities.CreateFactory for run-time arguments, and disposal and lifetime pitfalls.
Mid-Level
Coding
DI Core Concepts
Β· π 3 follow-ups Β· π 1 real-world
-
When is the built-in Microsoft.Extensions.DependencyInjection container not enough? Compare it with Autofac, Lamar and Scrutor: what features does the built-in container deliberately leave out, and when would you bring in another container or library?
Compares the built-in .NET container with Autofac, Lamar and Scrutor: missing features (property injection, scanning, decorators, child containers, implicit Func/Lazy), how third-party containers plug in with UseServiceProviderFactory, and when switching is justified.
Intermediate
Theory
DI Core Concepts
Β· π 3 follow-ups
-
You are writing a NuGet library that other teams will use in their ASP.NET Core apps. How should it integrate with dependency injection? Explain AddMyLibrary() extension methods, options, TryAdd, builder objects, and how you avoid tying consumers to one container.
Covers DI design for reusable .NET libraries: IServiceCollection extension methods, options and validation, TryAdd and TryAddEnumerable for overridable defaults, builder objects for chained configuration, depending only on abstractions packages, and lifetime choices.
Senior
Theory
DI Core Concepts
Β· π 3 follow-ups Β· π 1 real-world
-
How do you use dependency injection in tests? Show how you replace services in an ASP.NET Core integration test with WebApplicationFactory and ConfigureTestServices, when you use fakes versus mocks, and why unit tests usually shouldn't need a container at all.
Shows testing with DI: constructor-injected unit tests without a container, WebApplicationFactory with ConfigureTestServices to replace services, RemoveAll and Replace, fakes vs mocks, FakeTimeProvider, and testing the composition root with ValidateOnBuild.
Mid-Level
Coding
DI Core Concepts
Β· π 3 follow-ups Β· π 1 real-world
-
Code review: an OrderService constructor takes 12 injected dependencies (repositories, email, SMS, PDF, tax, discount, audit, cache, logger and more), and its methods also new up a TaxCalculator and an HttpClient. What problems do you see, ranked by severity, and how would you refactor it?
Reviews a class with constructor over-injection and hidden new-ed dependencies: SRP violations, socket exhaustion from new HttpClient, untestable collaborators, and refactoring into focused handlers, domain events and facade services.
Mid-Level
CodeReview
DI Core Concepts
Β· π 3 follow-ups Β· π 1 real-world
-
You join a team with a large legacy .NET application full of static helper classes, a hand-rolled ServiceLocator.Get<T>() and singletons that read configuration directly. How would you introduce dependency injection without a big-bang rewrite? Explain the composition root, the order you would tackle things in, and how you would keep the app shippable throughout.
A real-world strategy for introducing DI into a legacy .NET app: creating a composition root, wrapping statics behind interfaces, bridging the service locator to the container, strangling it incrementally, characterisation tests, and measuring progress.
Senior
RealWorldScenario
DI Core Concepts
Β· π 3 follow-ups Β· π 1 real-world
-
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