Understanding the Differences between Singleton, Scoped, and Transient Service Lifetime in .NET

To understand the difference between Singleton, Scoped, and Transient service lifetimes it is important to understand the life cycle of Dependency Injection.

The life cycle of Dependency Injection (DI) refers to the process by which an object or service is created and managed by a Dependency Injection (DI) container. A Dependency Injection (DI) container is a software component that manages the creation and lifetime of services in an application.

The basic steps of the Dependency Injection (DI) life cycle include:

  1. Registration: The application registers services that it needs with the Dependency Injection (DI) container.
  2. Resolution: If a service is requested, the DI container creates and returns an instance of the requested service. This process is called resolution.
  3. Injection: When the service is resolved, the DI container will then inject any dependencies that the object or service has into it.
  4. Lifetime management: The DI container is responsible for managing the lifetime of the services it creates. The lifetime can be a singleton, scoped, or transient.
  5. Disposal: The DI container is also responsible for disposing of services when they are no longer needed.

A Dependency Injection (DI) container will ensure that services are created and managed in a consistent and predictable way. It will also make sure that the dependencies between services are properly managed.

Now, let us talk about lifetime management, and how different lifetimes are managed by Dependency Injection containers.

In .NET, there are several ways to define the lifetime of a service when using dependency injection (DI). Singleton, Scoped, and Transient are three ways to define the lifetime of a service when using dependency injection (DI) in a web API.

Singleton

A singleton service is created only once by the DI container and is then reused for all subsequent requests. Singleton lifetime is useful for services that are stateless or have a long lifetime.

Singleton is like going into a library where you can read a book for as long as you want. It’s good for books that you always love to read, like one of your favorite classics.

Examples of this could be a service that implements caching, a service that logs the application’s behavior, etc.

To configure a singleton service in .NET you can use the AddSingleton method:

services.AddSingleton<ICacheService, CacheService>();
services.AddSingleton<ILogService, LogService>();
Scoped

A scoped service is created once per request. This lifetime is useful for services that are stateful and only need to be created once per request.

Scoped is like having a book that you read each time you go to the library, but you have to give it back once you read it. If you want to read it again you can request the book and a new copy will be given.

Examples of this could be a service that holds the current user’s information, a service that holds tracking information, etc.

To configure a scoped service in .NET you can use the AddScoped method:

services.AddScoped<IUserService, UserService>();
services.AddScoped<ITrackingService, TrackingService>();
Transient

A transient service is created each time it is requested by the DI container. This lifetime is useful for services that are stateless and have a short lifetime.

Transient is like going to a library and being allowed to read a book for a while, which you have to then give back and pick a new book to read.

Examples of this could be a service that generates a random number, a service that generates a unique identifier, a service that generates a timestamp, etc.

To configure a transient service in .NET you can use the AddTransient method:

services.AddTransient<INumberService, NumberService>();
services.AddTransient<IKeyService, Keyervice>();
services.AddTransient<ITimeService, TimeService>();

As a developer, you get to choose the service lifetime based on the service’s usage and behavior. In general, Singleton is the lifetime that is used less often, because most of the services should be stateless and Scoped or Transient should be used depending on the service’s use case.

Image by KamranAydinov on Freepik


Enjoyed this post? Subscribe to my YouTube channel for more great content. Your support is much appreciated. Thank you!


Check out my Udemy profile for more great content and exclusive learning resources! Thank you for your support.
Ervis Trupja - Udemy



Enjoyed this blog post? Share it with your friends and help spread the word! Don't keep all this knowledge to yourself.