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 lifetime should be used for a service that maintains shared state across all requests?
Answer: Singleton
Singleton services are instantiated once for the application lifetime, making them appropriate for shared, thread-safe state.
What is the default DI container used in ASP.NET Core?
Answer: Microsoft.Extensions.DependencyInjection
ASP.NET Core ships with its own lightweight DI container in the Microsoft.Extensions.DependencyInjection package.
How is scope validation enabled in ASP.NET Core DI to detect captive dependencies?
Answer: Set ValidateScopes = true in ServiceProviderOptions
Setting ValidateScopes = true (default in Development) causes the container to throw when a captive dependency is detected.
Which method lets you replace an existing service registration in ASP.NET Core DI?
Answer: services.Replace(ServiceDescriptor.Scoped())
services.Replace() removes the existing descriptor for the service type and adds the new one.
What happens if a service is not registered and GetService() is called?
Answer: It returns null
Unlike GetRequiredService, GetService() returns null when the service type is not registered.
Which interface allows a service to perform cleanup when the DI scope is disposed?
Answer: IDisposable
Implementing IDisposable on a scoped or transient service causes the DI container to call Dispose() when the scope ends.