DEV Community

Cover image for Difference between AddScoped, AddTransient and AddSingleton in .netcore
Neer S
Neer S

Posted on

Difference between AddScoped, AddTransient and AddSingleton in .netcore

In .NET Core (now called .NET 5 and later), when you register services in the dependency injection (DI) container, you have different options for specifying how instances of those services are created and managed. The three main lifetime options are:

  1. Singleton: Only one instance of the service is created for the entire application, and it is reused for each request.
    services.AddSingleton();

  2. Scoped: A new instance of the service is created for each HTTP request within the scope of that request.
    services.AddScoped();

  3. Transient: A new instance of the service is created every time it is requested.
    services.AddTransient();

Additionally, there is also a method called TryAddSingleton. This method is similar to AddSingleton, but it only adds the service if it hasn't been registered before. This can be useful when you want to ensure that a particular service is only registered once.
services.AddTransient();

Here's a brief summary of each:

  • Singleton:
     - One instance for the entire application.
     - Shared across all requests.
     - Useful for stateless services or services that can be shared safely.

  • Scoped:
     - One instance per HTTP request.
     - Shared within the scope of an HTTP request.
     - Useful for services that need to maintain state within the context of a single request.

  • Transient:
     - A new instance every time it is requested.
     - No sharing between different parts of the application.
     - Useful for lightweight, stateless services.

  • TryAddSingleton:
     - Adds the service as a singleton if it hasn't been registered before.
     - Useful to avoid unintentional duplicate registrations.

Choosing the appropriate lifetime for your services depends on the specific requirements of your application and how you want instances of those services to be managed and shared.

Billboard image

Monitoring as code

With Checkly, you can use Playwright tests and Javascript to monitor end-to-end scenarios in your NextJS, Astro, Remix, or other application.

Get started now!

Top comments (0)

Heroku

This site is powered by Heroku

Heroku was created by developers, for developers. Get started today and find out why Heroku has been the platform of choice for brands like DEV for over a decade.

Sign Up

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay