Introduction
When you build applications using ASP.NET Core, one of the most important concepts you will work with is Dependency Injection (DI). It helps you manage how objects (services) are created and used in your application.
A key part of Dependency Injection in ASP.NET Core is service lifetimes. These lifetimes define how long a service instance should live and how often it should be created.
The three main service lifetimes are:
AddTransient
AddScoped
AddSingleton
Understanding the difference between these is very important for building scalable, high-performance, and maintainable ASP.NET Core applications.
In this article, we will explain each one in simple words with examples and real-world use cases.
What is Dependency Injection in ASP.NET Core?
Dependency Injection is a design pattern used to reduce tight coupling between classes.
In simple words:
Instead of creating objects manually
You ask the framework to provide them
ASP.NET Core has a built-in DI container that manages services automatically.
Example:
builder.Services.AddTransient<IMyService, MyService>();
Here:
IMyServiceis the interfaceMyServiceis the implementation
Now ASP.NET Core will create and inject this service wherever needed.
What is Service Lifetime?
Service lifetime defines:
When a service is created
How long it is reused
When it is destroyed
Choosing the correct lifetime is very important for:
Performance optimization
Memory management
Avoiding bugs and unexpected behavior
AddTransient in ASP.NET Core
What is AddTransient?
AddTransient creates a new instance every time the service is requested.
Example
builder.Services.AddTransient<IMyService, MyService>();
How it works
Every injection → new object is created
No reuse of instances
Real-life analogy
Think of it like:
Ordering a new coffee every time you visit a café
You always get a fresh cup
When to use AddTransient
Lightweight services
Stateless services (no data stored inside)
Services that do not need to share data
Example scenario
Email service
Logging formatter
Utility/helper classes
Important point
Using too many transient services can increase object creation overhead.
AddScoped in ASP.NET Core
What is AddScoped?
AddScoped creates one instance per request.
Example
builder.Services.AddScoped<IMyService, MyService>();
How it works
One HTTP request → one instance
Same instance reused within that request
Real-life analogy
Think of it like:
A shopping cart in an e-commerce website
One cart per user session/request
When to use AddScoped
Database operations (like DbContext)
Services that need to share data within a request
Business logic services
Example scenario
Entity Framework DbContext
User session data handling
API request processing
Important point
Scoped services are the most commonly used in ASP.NET Core Web APIs.
AddSingleton in ASP.NET Core
What is AddSingleton?
AddSingleton creates only one instance for the entire application lifetime.
Example
builder.Services.AddSingleton<IMyService, MyService>();
How it works
Created once when the application starts
Same instance used everywhere
Join the conversation! Your thoughts help the community grow.