c# - AddTransient, AddScoped and AddSingleton Services Differences

ID : 732

viewed : 209

Tags : c#asp.net-corec#

Top 5 Answer for c# - AddTransient, AddScoped and AddSingleton Services Differences

vote vote



Transient objects are always different; a new instance is provided to every controller and every service.

Scoped objects are the same within a request, but different across different requests.

Singleton objects are the same for every object and every request.

For more clarification, this example from .NET documentation shows the difference:

To demonstrate the difference between these lifetime and registration options, consider a simple interface that represents one or more tasks as an operation with a unique identifier, OperationId. Depending on how we configure the lifetime for this service, the container will provide either the same or different instances of the service to the requesting class. To make it clear which lifetime is being requested, we will create one type per lifetime option:

using System;  namespace DependencyInjectionSample.Interfaces {     public interface IOperation     {         Guid OperationId { get; }     }      public interface IOperationTransient : IOperation     {     }      public interface IOperationScoped : IOperation     {     }      public interface IOperationSingleton : IOperation     {     }      public interface IOperationSingletonInstance : IOperation     {     } } 

We implement these interfaces using a single class, Operation, that accepts a GUID in its constructor, or uses a new GUID if none is provided:

using System; using DependencyInjectionSample.Interfaces; namespace DependencyInjectionSample.Classes {     public class Operation : IOperationTransient, IOperationScoped, IOperationSingleton, IOperationSingletonInstance     {         Guid _guid;         public Operation() : this(Guid.NewGuid())         {          }          public Operation(Guid guid)         {             _guid = guid;         }          public Guid OperationId => _guid;     } } 

Next, in ConfigureServices, each type is added to the container according to its named lifetime:

services.AddTransient<IOperationTransient, Operation>(); services.AddScoped<IOperationScoped, Operation>(); services.AddSingleton<IOperationSingleton, Operation>(); services.AddSingleton<IOperationSingletonInstance>(new Operation(Guid.Empty)); services.AddTransient<OperationService, OperationService>(); 

Note that the IOperationSingletonInstance service is using a specific instance with a known ID of Guid.Empty, so it will be clear when this type is in use. We have also registered an OperationService that depends on each of the other Operation types, so that it will be clear within a request whether this service is getting the same instance as the controller, or a new one, for each operation type. All this service does is expose its dependencies as properties, so they can be displayed in the view.

using DependencyInjectionSample.Interfaces;  namespace DependencyInjectionSample.Services {     public class OperationService     {         public IOperationTransient TransientOperation { get; }         public IOperationScoped ScopedOperation { get; }         public IOperationSingleton SingletonOperation { get; }         public IOperationSingletonInstance SingletonInstanceOperation { get; }          public OperationService(IOperationTransient transientOperation,             IOperationScoped scopedOperation,             IOperationSingleton singletonOperation,             IOperationSingletonInstance instanceOperation)         {             TransientOperation = transientOperation;             ScopedOperation = scopedOperation;             SingletonOperation = singletonOperation;             SingletonInstanceOperation = instanceOperation;         }     } } 

To demonstrate the object lifetimes within and between separate individual requests to the application, the sample includes an OperationsController that requests each kind of IOperation type as well as an OperationService. The Index action then displays all of the controller’s and service’s OperationId values.

using DependencyInjectionSample.Interfaces; using DependencyInjectionSample.Services; using Microsoft.AspNetCore.Mvc;  namespace DependencyInjectionSample.Controllers {     public class OperationsController : Controller     {         private readonly OperationService _operationService;         private readonly IOperationTransient _transientOperation;         private readonly IOperationScoped _scopedOperation;         private readonly IOperationSingleton _singletonOperation;         private readonly IOperationSingletonInstance _singletonInstanceOperation;          public OperationsController(OperationService operationService,             IOperationTransient transientOperation,             IOperationScoped scopedOperation,             IOperationSingleton singletonOperation,             IOperationSingletonInstance singletonInstanceOperation)         {             _operationService = operationService;             _transientOperation = transientOperation;             _scopedOperation = scopedOperation;             _singletonOperation = singletonOperation;             _singletonInstanceOperation = singletonInstanceOperation;         }          public IActionResult Index()         {             // ViewBag contains controller-requested services             ViewBag.Transient = _transientOperation;             ViewBag.Scoped = _scopedOperation;             ViewBag.Singleton = _singletonOperation;             ViewBag.SingletonInstance = _singletonInstanceOperation;              // Operation service has its own requested services             ViewBag.Service = _operationService;             return View();         }     } } 

Now two separate requests are made to this controller action:

First Request

Second Request

Observe which of the OperationId values varies within a request, and between requests.

  • Transient objects are always different; a new instance is provided to every controller and every service.

  • Scoped objects are the same within a request, but different across different requests

  • Singleton objects are the same for every object and every request (regardless of whether an instance is provided in ConfigureServices)

vote vote


In .NET's dependency injection there are three major lifetimes:

Singleton which creates a single instance throughout the application. It creates the instance for the first time and reuses the same object in the all calls.

Scoped lifetime services are created once per request within the scope. It is equivalent to a singleton in the current scope. For example, in MVC it creates one instance for each HTTP request, but it uses the same instance in the other calls within the same web request.

Transient lifetime services are created each time they are requested. This lifetime works best for lightweight, stateless services.

Here you can find and examples to see the difference:

ASP.NET 5 MVC6 Dependency Injection in 6 Steps (web archive link due to dead link)

Your Dependency Injection ready ASP.NET : ASP.NET 5

And this is the link to the official documentation:

Dependency injection in ASP.NET Core

vote vote


Which one to use


  • 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.


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


  • 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.)

vote vote


Transient, scoped and singleton define object creation process in ASP.NET MVC core DI(Dependency Injection) when multiple objects of the same type have to be injected. In case you are new to dependency injection you can see this DI IoC video.

You can see the below controller code in which I have requested two instances of "IDal" in the constructor. Transient, Scoped and Singleton define if the same instance will be injected in "_dal" and "_dal1" or different.

public class CustomerController : Controller {     IDal dal = null;      public CustomerController(IDal _dal,                               IDal _dal1)     {         dal = _dal;         // DI of MVC core         // inversion of control     } } 

Transient: In transient, new object instances will be injected in a single request and response. Below is a snapshot image where I displayed GUID values.

Enter image description here

Scoped: In scoped, the same object instance will be injected in a single request and response.

Enter image description here

Singleton: In singleton, the same object will be injected across all requests and responses. In this case one global instance of the object will be created.

Below is a simple diagram which explains the above fundamental visually.

enter image description here

The above image was drawn by the SBSS team when I was taking ASP.NET MVC training in Mumbai. A big thanks goes to the SBSS team for creating the above image.

vote vote


This image illustrates this concept well. Unfortunately, I could not find the source of this image, but someone made it, he has shown this concept very well in the form of an image. enter image description here

Update: Image reference : ASP.NET Core Service Lifetimes (Infographic) , Author: @WaqasAnwar

Top 3 video Explaining c# - AddTransient, AddScoped and AddSingleton Services Differences