Header add

In this article we will cover a important chapter of the built-in DI container Singleton, Scoped and Transient. We discussed the below points
>> What is Singleton, Scoped and Transient?
>> Difference between AddSingleton vs AddScoped vs AddTransient with example ?
  • Singleton means only a single instance will ever be created. That instance is shared between all components that require it. The same instance is thus used always.
  • Scoped means an instance is created once per scope. A scope is created on every request to the application, thus any components registered as Scoped will be created once per request.
  • Transient The services created using transient lifetime will be created each time they are requested. This lifetime works best for lightweight services.

ASP.NET core provides the following 3 methods to register services with the dependency injection container.
  1. AddSingleton() - A Singleton service is created only one time per application and that single instance is used throughout the application life time. As the name suggest,  AddSingleton() method creates a Singleton service. A Singleton service is created when it is first requested. This same instance is then used by all the subsequent requests. So in general, 
  2. AddTransient() - This method creates a Transient service. A new instance of a Transient service is created each time it is requested. 
  3. AddScoped() - This method creates a Scoped service. A new instance of a Scoped service is created once per request within the scope. For example, in a web application it creates 1 instance per each http request but uses the same instance in the other calls within that same web request.
Let's create a ASP .NET Core MVC application and do discuss about all the DI Container process flow in detail. Open VS ⇨ Choose ASP .NET Core Web ⇒ Choose MVC application

Create a simple POCO class which contains few properties describing the Mobile object.

Now create an Interface named IMobileService. Add() method adds a new mobile to the repository. GetAll() method returns all the mobiles in the repository.

MobileService implements the IMobileService. To keep the example simple we are storing the list of mobile data in-memory in a private field _mobileList. 

IMobileService is injected in to the HomeController. The Create() action method that responds to the POST request, uses the injected instance to add the mobile object to the repository.

Create View

Injecting the IMobileService service into the Create view using @inject directive. We are using the injected service to display the total number of mobiles in the service list.

Now we gonna implement the dependency container of AddSingleton(),AddScoped() and AddTransient ()

AddSingleton(): Services are registered in ConfigureServices() method of the Startup.cs file.

  • AddSingleton() creates a single instance of the service when it is first requested and reuses that same instance in all the places where that service is needed.
  • When we add the new mobile information and hit the create button it increment the value, if we hit button again and again it increment each time even on page refresh the increment value is not reset, this is because with Singleton, the same object is used, so changes made to the object can be viewed in all the places across all the HTTP requests.
AddScoped():  A new instance of a Scoped service is created once per request within the scope



  • When we hit the create button it add the increment value 1, means the highlighted count should 3 to 4 but if we hit again and again the value won't change anything because for a scoped service with every HTTP request we get a new instance. However, with in the same HTTP request if the service is required in multiple places like in the view and in the controller then the same instance is provided for the entire scope of that HTTP request.
  • If the page is refreshed the HTTP request create a new instance and the value is reset again.
  • Every time we click the Create button we are issuing a new HTTP request and hence the Total Mobile data count does not go beyond 4.
AddTransient ():  A new instance of a Transient service is created each time it is requested.



  • When you hit the button the value won't change it remains count as 3 this is because with a transient service a new instance is provided every time a service instance is requested whether it is in the scope of the same HTTP request or across different HTTP requests.  
  • When we hit the enter the create button the POST method is called and the mobile data count is 4 but it return to same view page as the transient service definition says each time the new instance of a Transient service is created that's why the value won't be change.

Service TypeIn the scope of same Http requestDifferent Http request
SingletonThe same Instance of repository servedThe same Instance of repository served
ScopedSame InstanceDifferent Instance
TransientEvery time New InstanceEvery time New Instance


Finally, a obvious question in mind that how to damn sure that when to use what service.

Transient would be used when the component cannot be shared. A non-thread-safe database access object would be one example. Transient services should be the default. Constantly re-creating services reduces the risk of a faulty implementation bringing everything down, since every new instance will be created in a non-faulty state.


Singletons should rarely be used. One use-case is if you have a global store of some kind, e.g. a cache, which should be shared between requests. Note that you have to be mindful of thread-safety using it.

Scoped can be used for Entity Framework database contexts. The main reason is that then entities gotten from the database will be attached to the same context that all components in the request see. Of course if you plan on doing queries with it in parallel, you can't use Scoped.

Usually we use scoped services to ensure some processing is done only once per request. At our current job, we use them to fetch once per request, and then that service is re-used at multiple points in the code (e.g. the repository for accessing the database, some HTTP-client accessing a back-end service) which are all transient.




</> Find the Source Code in Github.com/CoreProgramm/

   Summary
In this tutorial we discussed difference between AddSingleton vs AddScoped vs AddTransient in asp.net core. If have any question related to this topic then give your feedback.

Post a Comment

Previous Post Next Post