Dependency injection lifecycle in asp dot net core

In dependency injection (DI) in .NET, AddSingleton, AddScoped, and AddTransient are methods used to register services with different lifetimes. The lifetime determines how long an instance of a service is maintained and reused. Here's a breakdown of each:

1. AddSingleton Lifetime: A single instance of the service is created and shared throughout the application's lifetime.

Usage: Use this when you want a single instance of a service to be used across the entire application, such as for configuration settings, logging, or caching.

Example:


services.AddSingleton<IMyService, MyService>();
    

Behavior:

  • The same instance is returned every time the service is requested.
  • The instance is created the first time it is requested and then reused for all subsequent requests.

2. AddScoped Lifetime: A new instance of the service is created once per client request (or per scope). Usage: Use this for services that should be shared within a single request or operation, such as database contexts or unit-of-work patterns. Example:


services.AddScoped<IMyService, MyService>();
    
Behavior:
  • The same instance is shared within a single request or scope.
  • A new instance is created for each new request or scope.

3. AddTransient Lifetime: A new instance of the service is created every time it is requested. Usage: Use this for lightweight, stateless services where a new instance is needed for each operation, such as utility services or factories. Example:


services.AddTransient<IMyService, MyService>();

Behavior:
  • A new instance is created every time the service is requested.
  • No instance is reused; each request gets a fresh instance.

Key Differences
Lifetime Instance Created Reused Within Typical Use Cases
Singleton Once for the entire application Application lifetime Configuration, logging, caching
Scoped Once per request/scope Request/scope lifetime Database contexts, unit-of-work
Transient Every time it is requested Never (new instance each time) Lightweight, stateless services
Example Scenario

Imagine you have a service IMyService that logs messages:

  • Singleton: If registered as a singleton, the same logger instance will be used across the entire application, even across different requests.
  • Scoped: If registered as scoped, a new logger instance will be created for each HTTP request, but the same instance will be reused within that request.
  • Transient: If registered as transient, a new logger instance will be created every time the service is injected, even within the same request.
Choosing the Right Lifetime
  • Use Singleton for shared, stateless services that are expensive to create.
  • Use Scoped for services that need to maintain state within a single request or operation.
  • Use Transient for lightweight, stateless services that are cheap to create.

By understanding these lifetimes, you can optimize resource usage and avoid issues like memory leaks or unintended sharing of state.

An unhandled error has occurred. Reload 🗙