In this article, we will learn about AddTransient, AddScoped, and AddSingleton in .NET Core.
DI (Dependency Injection) is a technique for achieving loose coupling between objects and their dependencies. To implement Dependency Injection, we need to configure a DI container with classes that are participating in DI. DI Container has to decide whether to provide an existing instance or return a new instance of the service. In Startup.cs class file, we perform this activity on the ConfigureServices() method.
The lifetime of service depends on when a dependency is instantiated and how long it lives. And lifetime depends on how we have registered these services.
There are three methods that define the lifetime of the services in Startup.cs.
1. AddTransient 2. AddScoped 3. AddSingleton
Parameter | Add Transient | Add Scoped | Add Singleton |
---|---|---|---|
Meaning | Transient lifetime services are created each time when they are requested. | Scoped lifetime services are created once per request. | Singleton lifetime services are created only the first time when they are requested (or when ConfigureServices is run if you specify an instance there). |
Description | It means the Transient lifetime service creates a new instance for every controller/service as well as for every request and every user. | It means the Scoped objects are the same within a request, but different across different requests. | It means the Singleton lifetime service creates the instance for the first request then the same is available throughout the application and for each subsequent request. |
Used In | This lifetime works best for lightweight and stateless services. | This lifetime works best for applications which have different behavior per user. | This lifetime used, when Singleton implementation is required. |
Syntax | services.AddTransient() | services.AddScoped() | services.AddSingleton() |
Disposed | End of request | End of request | App shutdown |
Service Type | In the scope of given HTTP request | Across different HTTP requests |
---|---|---|
Transient | New Instance | New Instance |
Scoped | Same Instance | New Instance |
Singleton | Same Instance | Same Instance |
Example
Prerequisites:
- Basic knowledge of .NET Core
- Code editor like Visual Studio
Create a folder named Interfaces and add a new file IOperation.cs in it.
using System; namespace DependencyInjectionDemo.Interfaces { public interface IOperation { Guid OperationId { get; } } public interface IOperationTransient : IOperation { } public interface IOperationScoped : IOperation { } public interface IOperationSingleton : IOperation { } }
Create a folder named Classes and add a new file Operation.cs in it.
using DependencyInjectionDemo.Interfaces; using System; namespace DependencyInjectionDemo.Classes { public class Operation : IOperationTransient, IOperationScoped, IOperationSingleton { Guid guid; public Operation() : this(Guid.NewGuid()) { } public Operation(Guid _guid) { guid = _guid; } public Guid OperationId => guid; } }
Now, open the Startup.cs file and add the following code to in ConfigureServices() method.
public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddTransient<IOperationTransient, Operation>(); services.AddScoped<IOperationScoped, Operation>(); services.AddSingleton<IOperationSingleton, Operation>(); }
Now, open the HomeController and add the following code to it.
using DependencyInjectionDemo.Interfaces; using Microsoft.AspNetCore.Mvc; namespace DependencyInjectionDemo.Controllers { public class HomeController : Controller { private readonly IOperationTransient _transientOperation1; private readonly IOperationScoped _scopedOperation1; private readonly IOperationSingleton _singletonOperation1; private readonly IOperationTransient _transientOperation2; private readonly IOperationScoped _scopedOperation2; private readonly IOperationSingleton _singletonOperation2; public HomeController(IOperationTransient transientOperation1, IOperationScoped scopedOperation1, IOperationSingleton singletonOperation1, IOperationTransient transientOperation2, IOperationScoped scopedOperation2, IOperationSingleton singletonOperation2) { _transientOperation1 = transientOperation1; _scopedOperation1 = scopedOperation1; _singletonOperation1 = singletonOperation1; _transientOperation2 = transientOperation2; _scopedOperation2 = scopedOperation2; _singletonOperation2 = singletonOperation2; } public IActionResult Index() { //Controller Operation ViewBag.Transient1 = _transientOperation1.OperationId.ToString(); ViewBag.Scoped1 = _scopedOperation1.OperationId.ToString(); ViewBag.Singleton1 = _singletonOperation1.OperationId.ToString(); //Operation Service Operation ViewBag.Transient2 = _transientOperation2.OperationId.ToString(); ViewBag.Scoped2 = _scopedOperation2.OperationId.ToString(); ViewBag.Singleton2 = _singletonOperation2.OperationId.ToString(); return View(); } } }
Output:
Please give your valuable feedback and if you have any questions or issues about this article, please let me know.
Our ASP.NET experts can assist you in creating a cloud-first software application that provides exceptional outcomes and efficiency in the lowest amount of time. Hiring dedicated .Net Developers who welcome challenges and use creative approaches to their work is a good idea. We produce exceptional outcomes by customizing our solutions based on our client’s individual requirements and goals, with over sixty experienced professionals involved in multi-skilled disciplines.
Also, check Allow CORS Requests From Any Origin In .NET Core