ASP.NET Core Interview

Dependency Injection & Services

ASP.NET Core interview questions covering dependency injection & services.

25 Questions Filter by level

Interview questions

Open a question for the full answer, code and interview guidance.

25 Results
51
JuniorBeginner

What is dependency injection in ASP.NET Core?

Dependency injection (DI) is a design technique in which a class receives the objects it depends on instead of creating those objects itself. ASP.NET Core includes a built-in dependency injection container.

52
JuniorBeginner

Why is dependency injection important in ASP.NET Core applications?

Dependency injection separates a class from the concrete construction of its dependencies. Instead of a controller creating repositories, loggers, or services with new, those dependencies are supplied by the container.

53
JuniorBeginner

What is IServiceCollection?

IServiceCollection is the collection used to register services with ASP.NET Core's dependency injection container before the application is built.

54
JuniorBeginner

What is IServiceProvider?

IServiceProvider is the runtime service resolver built from the registrations in IServiceCollection. ASP.NET Core uses it internally to create objects and satisfy their registered dependencies.

55
JuniorBeginner

What are Transient, Scoped, and Singleton service lifetimes?

ASP.NET Core supports three primary DI lifetimes.

56
JuniorBeginner

When should you use a Transient service?

Use a Transient lifetime when a service is lightweight, stateless, inexpensive to construct, and should not retain state between resolutions.

57
JuniorBeginner

When should you use a Scoped service?

A Scoped service is appropriate when one instance should be shared within a single request or logical operation but not across unrelated requests.

58
JuniorBeginner

When should you use a Singleton service?

A Singleton service is created once and reused for the lifetime of the application process. It is appropriate for services that are expensive to construct, hold application-wide immutable state, or safely manage shared state.

59
JuniorBeginner

What is a captive dependency?

A captive dependency occurs when a longer-lived service holds a dependency with a shorter lifetime, most commonly when a Singleton directly depends on a Scoped service.

60
JuniorBeginner

What is constructor injection?

Constructor injection supplies dependencies through a class constructor. It is the most common DI technique in ASP.NET Core and is generally preferred for required dependencies.

61
JuniorBeginner

What is method or action injection in ASP.NET Core?

Method injection supplies a dependency directly to a method rather than storing it on the class. In MVC controllers, an action parameter can request a registered service with the FromServices attribute.

62
JuniorBeginner

What is keyed dependency injection in ASP.NET Core?

Keyed dependency injection allows multiple implementations of the same service type to be registered and selected by a key. This is useful when an application has several strategies that implement one interface.

63
JuniorBeginner

How do you register an interface and implementation in ASP.NET Core DI?

The common registration form maps an abstraction to a concrete implementation using AddTransient, AddScoped, or AddSingleton.

64
JuniorBeginner

How do you register a concrete service without an interface?

ASP.NET Core can register a concrete class directly. This is useful when there is only one meaningful implementation and an interface would not provide a useful abstraction.

65
JuniorBeginner

How can you register a service using a factory delegate?

A factory delegate allows custom logic to run when the DI container creates a service. The delegate receives an IServiceProvider, so it can resolve other registered services and configuration needed to construct the object.

66
JuniorBeginner

How do you register multiple implementations of the same interface?

Multiple implementations can be registered for the same service interface. When a consumer requests IEnumerable<TService>, the container supplies all registered implementations in registration order.

67
JuniorBeginner

What is TryAdd in ASP.NET Core dependency injection?

TryAdd extension methods register a service only when that service type has not already been registered. They are provided by Microsoft.Extensions.DependencyInjection.Extensions.

68
JuniorBeginner

What is Replace in ASP.NET Core dependency injection?

Replace removes the existing registration for a service type and adds a new ServiceDescriptor. It is useful when an application needs to replace a default registration supplied by a framework component or reusable library.

69
JuniorBeginner

What is IServiceScope and why is it used?

IServiceScope represents a dependency injection scope. Services registered as Scoped are shared within that scope and disposed when the scope is disposed.

70
JuniorBeginner

What is IServiceScopeFactory?

IServiceScopeFactory creates IServiceScope instances. It is especially useful in singleton services and background workers that need to resolve scoped dependencies for individual operations.

71
JuniorBeginner

How does ASP.NET Core dispose dependency-injected services?

The built-in DI container tracks disposable services that it creates and disposes them when the owning scope is disposed.

72
JuniorBeginner

Why should you avoid the Service Locator pattern?

The Service Locator pattern occurs when application classes depend on IServiceProvider and manually request their real dependencies at runtime.

73
JuniorBeginner

How does dependency injection improve unit testing?

DI makes dependencies replaceable. A class can be tested with a fake, stub, or mock implementation instead of connecting to a real database, external API, file system, or email provider.

74
JuniorBeginner

How can extension methods organize service registrations?

Extension methods can group related IServiceCollection registrations and keep Program.cs readable as an application grows.

75
JuniorBeginner

What are good dependency injection practices in ASP.NET Core?

Use constructor injection for required dependencies, choose lifetimes deliberately, and keep singleton services thread-safe. Avoid injecting scoped services into singletons and avoid using IServiceProvider as a general-purpose service locator.