Our latest


Announcements, Blogs, Releases and much much more...

  • Read our latest news and announcements, hot off the press.

Announcements






Category Article

Published on 28-May-2021

AddTransient, AddScoped and AddSingleton Services Differences
AddTransient, AddScoped and AddSingleton Services Differences
AddTransient, AddScoped and AddSingleton Services Differences

What is the difference between the services.AddTransient and service.AddScoped methods in ASP.NET Core?

Which one to use

Transient

  • since they are created every time they will use more memory & Resources and can have the negative impact on performance
  • use this for the lightweight service with little or no state.

Scoped

  • better option when you want to maintain state within a request.

Singleton

  • memory leaks in these services will build up over time.
  • also memory efficient as they are created once reused everywhere.

Use Singletons where you need to maintain application wide state. Application configuration or parameters, Logging Service, caching of data is some of the examples where you can use singletons.


Injecting service with different lifetimes into another

  1. Never inject Scoped & Transient services into Singleton service. ( This effectively converts the transient or scoped service into the singleton.)
  2. Never inject Transient services into scoped service ( This converts the transient service into the scoped. )


Created on 28-May-2021 Last updated 08-Jun-2021

Assigned Tag
  • netcore
  • Blazor