Give me difference between AddScoped, AddTransient and AddSingleton with an example?
Loading
Give me difference between AddScoped, AddTransient and AddSingleton with an example?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 21, 2024, 11:48 PM EST
In ASP.NET Core,
AddScoped,AddTransient, andAddSingletonare methods used to register services with different lifetimes in the dependency injection container. Here’s a brief explanation of each:AddTransient: This method creates a new instance of the service each time it is requested, regardless of whether it’s within the same HTTP request or a different one12.
AddScoped: This method creates a new instance of the service for each HTTP request. This means that within the same HTTP request, you’ll get the same instance of the service12.
AddSingleton: This method creates a single instance of the service when it is first requested (or when
ConfigureServicesis run if you specify an instance there). Every subsequent request will use the same instance2.Here’s an example of how you might register services using these methods:
public void ConfigureServices(IServiceCollection services) { services.AddTransient(); services.AddScoped(); services.AddSingleton(); }In this example,
OperationServiceis a class that implements three interfaces:ITransientService,IScopedService, andISingletonService. Each interface is registered with a different lifetime. When these services are requested (for example, via constructor injection in a controller), the dependency injection container will provide instances according to the lifetime specified inConfigureServices2.Reffer below link:
https://www.c-sharpcorner.com/article/understanding-addtransient-vs-addscoped-vs-addsingleton-in-asp-net-core/
Thanks
Vishal YelveRank #178 · 13.6K reputationPosted Jan 22, 2024, 1:56 AM EST
Hi Umesh,
// Singleton builder.Services.AddSingleton(); ???????// Scoped builder.Services.AddScoped(); // Transient builder.Services.AddTransient();Singleton creates a single instance once and reuses the same object in all calls. Use Singletons where you need to maintain pplication-wide state, for example, application configuration, logging service, caching of data, etc.
Scoped lifetime services are created once per request. 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. Since they are created every time, they will use more memory & resources and can have a negative impact on performance.
Also, refer to below Articles
https://www.c-sharpcorner.com/article/addtransient-vs-addscoped-vs-addsingleton/#:~:text=Stateful%20services%20that%20need%20to,can%20be%20registered%20as%20AddSingleton.
https://www.linkedin.com/pulse/differences-between-addtransient-addscoped-addsingleton-mia-7vg6c
Dhiraj PoojaryRank #1,039 · 1.9K reputationPosted Jan 24, 2024, 11:40 AM EST
Certainly! In .NET Core,
AddScoped,AddTransient, andAddSingletonare methods used to register services with the built-in dependency injection container. Here's a breakdown of the differences between them along with examples:AddScoped:services.AddScoped(); AddScopedare created once per request. That means a single instance of the service is created for the duration of an HTTP request. If the service is requested multiple times within the same request, the same instance will be reused.AddTransient:services.AddTransient(); AddTransientare created each time they are requested. This results in a new instance of the service being created for every request, ensuring freshness but potentially leading to a higher resource consumption.AddSingleton:services.AddSingleton(); AddSingletonare created only once and the same instance is used for the entire lifetime of the application. This means that the instance is shared across all requests and clients.Example Scenario:
Let's consider a simple scenario where you have a service that counts the number of times it has been instantiated:
public interface ICounter { int GetCount(); } public class Counter : ICounter { private int _count = 0; public Counter() { _count++; } public int GetCount() { return _count; } }Now, let's register this service using different lifetimes:
// Using AddScoped services.AddScoped(); // Using AddTransient services.AddTransient(); // Using AddSingleton services.AddSingleton();In this example:
AddScoped, the counter will increment once per HTTP request.AddTransient, the counter will increment every time the service is requested.AddSingleton, the counter will increment only once for the entire lifetime of the application, regardless of how many times the service is requested.