Dependency Injection and Services in ASP.NET Core: Essential Concepts Quiz Quiz

Explore key concepts related to dependency injection and service lifetimes in ASP.NET Core. This quiz is designed to help developers reinforce their understanding of service registration, dependency management, and related best practices in modern web applications.

  1. Identifying Dependency Injection Usage

    Which statement best describes dependency injection in an ASP.NET Core application?

    1. A process for explicitly instantiating every class at startup
    2. A method to directly create services using the new keyword
    3. A way to provide objects to classes through their constructors
    4. A technique for separating code into multiple files

    Explanation: Dependency injection supplies required objects, often called dependencies, to a class via its constructor or properties. Directly creating services using the new keyword bypasses the benefits of inversion of control. Instantiating every class at startup is inefficient and not related to dependency injection. Separating code into multiple files is about code organization, not dependency management.

  2. Service Lifetime Understanding

    Which service lifetime creates a new instance of the service every time it is requested?

    1. Transient
    2. Instant
    3. Scoped
    4. Singleton

    Explanation: Transient lifetime means a new object is created for each service request, which is appropriate for lightweight, stateless services. Scoped creates one instance per request, while singleton creates one shared instance throughout the application's lifetime. Instant is not a valid service lifetime in this context.

  3. Registration of Custom Services

    What is the correct method to register a custom service interface and its implementation as scoped?

    1. services.SetScopedu003CIMyService, MyServiceu003E()
    2. services.InjectScopedu003CIMyService, MyServiceu003E()
    3. services.AddSingletonu003CIMyService, MyServiceu003E()
    4. services.AddScopedu003CIMyService, MyServiceu003E()

    Explanation: AddScoped is the correct method for registering a service with a scoped lifetime, ensuring a single instance per request. InjectScoped and SetScoped are not valid registration methods, while AddSingleton registers the service as a singleton, which behaves differently.

  4. Constructor Injection Example

    Given 'public HomeController(IMyService service) {...}', how does ASP.NET Core provide 'IMyService'?

    1. By reading its name from an environment variable
    2. By resolving 'IMyService' from the service container
    3. By declaring it as a static instance in code
    4. By always creating a new unregistered instance

    Explanation: ASP.NET Core uses the dependency injection container to resolve and provide the requested service interface. Always creating new unregistered instances ignores the registration process. Environment variables are not used to provide services, and declaring a static instance is a different approach unrelated to the lifecycle management provided by dependency injection.

  5. Singleton Service Scenario

    Which scenario is best suited for a singleton service?

    1. A stateless configuration provider used across the entire app
    2. A user-specific shopping cart per HTTP request
    3. A short-lived random number generator for each call
    4. A time-limited cache that resets on every request

    Explanation: Singletons are ideal for services that maintain shared, unchanging state across the application, like configuration providers. User-specific shopping carts and time-limited caches often require request-based or transient lifetimes. Random number generators are not good candidates for singleton because they may need to be reset or re-seeded frequently.

  6. Dependency Injection Benefits

    What is one primary benefit of using dependency injection patterns?

    1. It prevents the need to use interfaces in the application
    2. It always improves the application’s runtime speed
    3. It allows classes to inherit from multiple base classes
    4. It reduces coupling between classes, making code more maintainable

    Explanation: Reducing class coupling via dependency injection makes code easier to test and maintain. While it may bring small performance benefits via better management, it is not mainly focused on speed. Interfaces are still useful and can be required in DI, and .NET does not support multiple inheritance of base classes, so that is unrelated.

  7. Accessing Built-in Services

    How does ASP.NET Core typically allow access to built-in services like logging?

    1. By loading them from configuration files at runtime
    2. By calling them directly as static global objects
    3. By declaring them as variables with private access
    4. By injecting them through constructor parameters in controllers and services

    Explanation: ASP.NET Core favors injecting dependencies such as logging or configuration into classes via constructors, promoting clear dependencies. Static global objects bypass DI, while private variables are not a means of acquiring services. Configuration files may contain settings, but do not directly provide service instances.

  8. Service Collection Modification

    Which method is used to add a service to the dependency injection container during application startup?

    1. services.AddTransientu003CTService, TImplementationu003E()
    2. container.InsertServiceu003CTService, TImplementationu003E()
    3. serviceHost.RegisterTransientu003CTService, TImplementationu003E()
    4. serviceProvider.Bindu003CTService, TImplementationu003E()

    Explanation: AddTransient is the standard way to add transient services in ASP.NET Core's service collection. InsertService, Bind, and RegisterTransient are not valid methods in this context and would not achieve the correct result.

  9. Resolving Multiple Implementations

    If multiple implementations of an interface are registered, how does ASP.NET Core typically resolve them?

    1. By injecting a Dictionary of the interface
    2. By throwing an error when the application starts
    3. By picking one at random every time
    4. By injecting an IEnumerable of the interface

    Explanation: Injecting an IEnumerable of the interface allows all registered implementations to be provided to the consuming class. Dictionary injection is not the standard, and picking an implementation at random is not a defined behavior. Registering several implementations does not cause an error, since ASP.NET Core supports this pattern.

  10. Service Resolution Location

    Where should services generally be resolved in ASP.NET Core applications?

    1. Within constructors or as method parameters via injection
    2. By calling GetService directly in business logic methods
    3. Within static fields for global access
    4. By using new keyword throughout the app

    Explanation: Services should be injected into constructors or as parameters, following inversion of control patterns. Calling GetService directly or using new undermines dependency injection principles. Relying on static fields can lead to issues with testability and lifecycle management.