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 method retrieves a required service and throws if it is not registered?
Answer: GetRequiredService()
GetRequiredService() throws InvalidOperationException if the service is not found, unlike GetService() which returns null.
How do you inject multiple implementations of the same interface in ASP.NET Core DI?
Answer: Inject IEnumerable in the constructor
ASP.NET Core DI resolves all registered implementations of an interface when IEnumerable is injected.
What is the purpose of the IServiceScope interface in ASP.NET Core?
Answer: It creates a child DI scope for resolving scoped services outside a request
IServiceScope provides a scoped IServiceProvider, used to resolve scoped services in background services or tests.
Which DI registration method allows providing a factory function instead of a type?
Answer: services.AddScoped(sp => new T(...))
The overload accepting Func allows custom construction logic using the service provider.
What is constructor injection in ASP.NET Core DI?
Answer: Declaring dependencies as constructor parameters that the DI container resolves automatically
Constructor injection is the primary DI pattern where ASP.NET Core resolves and passes dependencies automatically when creating the class.
What attribute enables property injection in ASP.NET Core Razor Pages?
Answer: [FromServices]
[FromServices] on a parameter or property tells the framework to resolve the value from the DI container rather than from the request.