โ† All ASP.NET Core Flashcard Decks

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
  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.