Introduction
In ASP.NET Core, Dependency Injection (DI) is a built-in feature that helps manage object creation and lifetime. When you register services in the DI container, you often see three common methods: AddTransient, AddScoped, and AddSingleton.
Understanding the difference between AddTransient vs AddScoped vs AddSingleton is very important for building scalable, high-performance, and bug-free applications. Choosing the wrong lifetime can lead to memory issues, unexpected behavior, or performance problems.
In this article, we will explain each lifetime in simple words, compare them clearly, and show real-world examples so you can confidently decide which one to use.
What is Dependency Injection in ASP.NET Core?
Dependency Injection is a design pattern used to achieve loose coupling between classes.
Instead of creating objects manually, the framework provides them when needed.
Example:
public interface IMessageService
{
void Send(string message);
}
public class EmailService : IMessageService
{
public void Send(string message)
{
Console.WriteLine($"Email Sent: {message}");
}
}
Registering in DI container:
builder.Services.AddTransient<IMessageService, EmailService>();
What is AddTransient?
AddTransient creates a new instance of a service every time it is requested.
Key Points of AddTransient
New object is created every time
Lightweight and short-lived
No shared state
Best for stateless services
Example of AddTransient
builder.Services.AddTransient<IMessageService, EmailService>();
If this service is injected into multiple places, each injection gets a new instance.
When to Use AddTransient
Stateless services
Lightweight operations
Services with no shared data
What is AddScoped?
AddScoped creates one instance per request.
In web applications, a "request" means a single HTTP request.
Key Points of AddScoped
One instance per request
Shared within the same request
New instance for each new request
Example of AddScoped
builder.Services.AddScoped<IMessageService, EmailService>();
All components within the same request share the same instance.
When to Use AddScoped
Database contexts (like DbContext)
Request-based operations
Services that need to share data within a request
What is AddSingleton?
AddSingleton creates only one instance for the entire application lifetime.
Key Points of AddSingleton
Single instance for entire app
Created once and reused
Shared across all requests
Must be thread-safe
Example of AddSingleton
builder.Services.AddSingleton<IMessageService, EmailService>();
All users and requests share the same instance.
When to Use AddSingleton
Configuration services
Caching services
Logging services
Expensive object creation
Join the conversation! Your thoughts help the community grow.