| NET-Core-Native-AOT-QA :👈 | 👉:ASP.NET-Interview-Questions-8 |
ASP.NET Interview Questions 71 to 90 |
Q71. Why is DbContext usually registered as Scoped?
DbContext represents a unit of work and is designed to be used within a short-lived scope. A request-scoped lifetime provides an appropriate boundary for database operations and avoids sharing the same context concurrently across unrelated requests.
Q72. Can a Singleton service depend on a Scoped service?
Not directly through its constructor. This creates a captive dependency. If the Singleton genuinely needs a scoped service, it should create a scope using IServiceScopeFactory and use the scoped dependency only within that scope.
Q73. What is a Captive Dependency?
A Captive Dependency occurs when a longer-lived service holds a shorter-lived dependency, causing the shorter-lived object to remain alive longer than intended.
Q74. What is IServiceCollection?
IServiceCollection is a collection of service registrations. It is used to register service types, implementations, factories, and lifetimes.
Q75. What is IServiceProvider?
IServiceProvider resolves registered services and creates the required dependency graph.
Q76. What is IServiceScopeFactory?
IServiceScopeFactory creates new DI scopes. It is commonly used in background services and Singleton services that need access to Scoped dependencies.
Q77. Why is constructor injection preferred?
Constructor injection makes dependencies explicit, ensures required dependencies are supplied during object creation, and makes unit testing easier.
Q78. Does ASP.NET Core support property injection by default?
The built-in DI container does not automatically support general property injection. Constructor injection is the usual approach.
Q79. What happens when multiple implementations of an interface are registered?
When a single service is requested, the default Microsoft DI container returns the last registered implementation. When IEnumerable of that interface is requested, it returns all registered implementations.
Q80. What are Open Generic Registrations?
Open Generic Registrations allow a generic interface and generic implementation to be registered once and resolved for multiple type arguments.
Q81. What are Keyed Services?
Keyed Services allow multiple implementations of the same service type to be registered with different keys. A consumer can request a particular implementation by specifying its key.
Q82. What is the Options Pattern?
The Options Pattern maps configuration values to strongly typed classes and makes them available through DI using interfaces such as IOptions, IOptionsSnapshot, and IOptionsMonitor.
Q83. What is the difference between IOptions and IOptionsSnapshot?
IOptions provides a stable options value, while IOptionsSnapshot provides options evaluated for a DI scope. IOptionsSnapshot is scoped and is useful for request-specific configuration evaluation.
Q84. What is the difference between IOptionsSnapshot and IOptionsMonitor?
IOptionsSnapshot is scoped and provides a value for the current scope. IOptionsMonitor supports observing updated options values and can be injected into Singleton services.
Q85. How do you inject a Scoped service into middleware?
For conventional middleware, inject the Scoped service into the Invoke or InvokeAsync method rather than the middleware constructor. Alternatively, implement IMiddleware and register it through DI.
Q86. How do you use Scoped services in a BackgroundService?
Inject IServiceScopeFactory, create a scope during processing, resolve the Scoped service from that scope, and dispose the scope when processing is complete.
Q87. Does the DI container dispose registered services?
Yes. The container tracks disposable services it creates and disposes them when their owning scope or provider is disposed. Objects supplied as pre-created instances are generally owned and disposed by the code that created them.
Q88. What is the Service Locator Pattern?
Service Locator is a pattern in which a class requests dependencies from a central provider instead of declaring them as constructor parameters. It can hide dependencies and make testing more difficult, so constructor injection is generally preferred.
Q89. Can we register multiple services with the same interface?
Yes. Multiple implementations can be registered. Resolve a single implementation when appropriate, use IEnumerable to obtain all registrations, or use Keyed Services when a specific implementation must be selected.
Q90. What is the difference between AddSingleton and AddSingleton with an instance?
AddSingleton with an implementation type or factory asks the container to create and manage the service instance. AddSingleton with an existing instance registers an object that has already been created, and its creator remains responsible for its disposal.
| NET-Core-Native-AOT-QA :👈 | 👉:ASP.NET-Interview-Questions-8 |