1/10
Looks like no tags are added yet.
What is the Transient service lifetime in .NET DI?
A new instance of the service is created every single time it is requested from the DI container.
How do you register a Transient service in ASP.NET Core?
Using builder.Services.AddTransient
When should you use a Transient service lifetime?
For lightweight, stateless services that require no shared state across operations or threads.
What is the Scoped service lifetime in .NET DI?
A single instance of the service is created once per client request or operation such as an HTTP request.
How do you register a Scoped service in ASP.NET Core?
Using builder.Services.AddScoped
When should you use a Scoped service lifetime?
For stateful per-request operations like Entity Framework DbContext, Unit of Work, or request-specific context.
What is the Singleton service lifetime in .NET DI?
A single instance is created the first time it is requested and stays alive for the entire application lifetime.
How do you register a Singleton service in ASP.NET Core?
Using builder.Services.AddSingleton
When should you use a Singleton service and what must you be careful of?
Use for shared thread-safe state such as caching or clocks; ensure thread-safety and avoid injecting shorter-lived services.
What is a Captive Dependency in .NET Dependency Injection?
A bug occurring when a longer-lived service like a Singleton holds a reference to a shorter-lived service like a Scoped or Transient service.
Why are Captive Dependencies dangerous in web applications?
The Scoped service is kept alive indefinitely by the Singleton, causing memory leaks, stale data, and multi-threading bugs across HTTP requests.