In this tutorial, we are going to discuss the different lifetime's services in .NET 6.0.
In Dependency Injection container, an object can be registered either of the three different ways,
- Transient
An object is created whenever they are requested from the container. It does not matter how many times the object is being called. Every time it is called, a new instance will be created, and it will be returned from the container. The transient objects are disposed at the end of request.
- Scoped
The scope lifetime basically indicates that the instance of the object that we are requesting from the container will be created once per the request lifecycle. Meaning, once per an HTTP request. Even if we call multiple times, the same instance will be returned across the lifetime of the request. The object will be disposed at the end of the request. We will investigate in detail regarding this in our example.
- Singleton
Object will be created only once for the entire lifetime of the application. Every subsequent request, container will return the same instance. We will see the details in a demonstration.
Note
One thing we need to keep in mind regarding how these different objects should be created or order of the operations. In terms of order of operations.
- A singleton service can be resolved from a singleton or scoped or transient service.
- A scoped service can be resolved from a scoped or transient service.
One thing we should be careful about is not to resolve a scoped service from a singleton service. Because it might have unwanted consequences on how we use the scoped service.
Without any delay, let us go ahead and create a ASP.NET Core Web API project.
The tools which I’m using for this demo are below,
- Visual Studio Community 2022(64 bit) – Preview - Version 17.4.0 Preview 2.1
- .Net 6.0
- Web API
- Swagger
- Postman
The source code can be downloaded from the GitHub
Let us create the below interfaces.
ICounter.cs
namespace LifetimeDemo.Interfaces {
public interface ICounter {
void Increment();
int GetCount();
}
}
IFirstInstance.cs
namespace LifetimeDemo.Interfaces {
public interface IFirstInstanace {
int IncrementAndGet();
}
}
ISecondInstance.cs
namespace LifetimeDemo.Interfaces {
public interface ISecondInstance {
int IncrementAndGet();
}
}
Now let us go ahead and create the implementation classes
Counter.cs
using LifetimeDemo.Interfaces;
namespace LifetimeDemo.Implementations {
public class Counter: ICounter {
private int _count;
public int GetCount() => _count;
public void Increment() => _count++;
}
}
FirstInstance.cs
using LifetimeDemo.Interfaces;
namespace LifetimeDemo.Implementations {
public class FirstInstance: IFirstInstanace {
private ICounter _counter;
public FirstInstance(ICounter counter) => _counter = counter;
public int IncrementAndGet() {
_counter.Increment();
return _counter.GetCount();
}
}
}
SecondInstance.cs
using LifetimeDemo.Interfaces;
namespace LifetimeDemo.Implementations {
public class SecondInstance: ISecondInstance {
private ICounter _counter;
public SecondInstance(ICounter counter) => _counter = counter;
public int IncrementAndGet() {
_counter.Increment();
return _counter.GetCount();
}
}
}
Now create a Controller CounterController.cs
using LifetimeDemo.Interfaces;
using Microsoft.AspNetCore.Mvc;
namespace LifetimeDemo.Controllers {
[Route("api/[Controller]")]
[ApiController]
public class CounterController: ControllerBase {
private readonly IFirstInstanace _first;
private readonly ISecondInstance _second;
public CounterController(ISecondInstance second, IFirstInstanace first) => (_second, _first) = (second, first);
[HttpGet]
public int Get() {
_first.IncrementAndGet();
return _second.IncrementAndGet();
}
}
}
In the CounterController.cs, if you look at the Method “Get”, I’m incrementing the count using the _first instance and, returning the value using the _second instance.







Abhishek YadavPosted Oct 19, 2022, 8:27 AM
Gud article keep it up
Innocent LangaPosted Oct 18, 2022, 11:49 AM
Thank you million times for making that clear distinction.