Dependency Injection Flashcards
6 cards from real ASP.NET Core practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 6 Dependency Injection flashcards as text
Which service lifetime registers a new instance for every HTTP request in ASP.NET Core?
Answer: Scoped
Scoped services are created once per HTTP request and shared within that request's scope.
What happens when a Singleton service depends on a Scoped service in ASP.NET Core?
Answer: It causes a captive dependency anti-pattern and may throw an exception
Injecting a shorter-lived scoped service into a singleton creates a captive dependency; with scope validation enabled, it throws at startup.
Which method registers a service with Transient lifetime in ASP.NET Core DI?
Answer: services.AddTransient()
AddTransient() registers the implementation to be created fresh each time it is requested.
What interface is used to access the DI service container manually in ASP.NET Core?
Answer: IServiceProvider
IServiceProvider exposes the GetService and GetRequiredService methods to resolve dependencies from the container.
Which ASP.NET Core method is used to register all services with the DI container?
Answer: ConfigureServices() in Startup or builder.Services in Program.cs
Services are registered in ConfigureServices() (classic Startup) or via builder.Services in the minimal hosting model.
What does services.AddScoped() do?
Answer: Registers ServiceImpl as the implementation for IService with scoped lifetime
This registers ServiceImpl to be resolved whenever IService is requested, creating one instance per scope.