AddScoped, AddTransient, AddSingleton
noun
say it: “add-SKOHPT, add-TRAN-zee-ent, add-SING-gul-tun” /ædˈskoʊpt ædˈtrænziənt ædˈsɪŋɡəltən/
The three ways to register a service in ASP.NET Core: a new one every time, one per request, or one for the whole app.
What is AddScoped, AddTransient, AddSingleton?
AddTransient creates a new instance every time; AddScoped one per HTTP request; AddSingleton one for the app's whole life. Never inject a scoped service into a singleton.
builder.Services.AddTransient<IPriceFormatter, PriceFormatter>();
builder.Services.AddScoped<IOrderService, OrderService>();
builder.Services.AddSingleton<IClock, SystemClock>();
In a sentence
- “We register the repository with AddScoped, so each request gets its own.”
Level: Apprentice · More ASP.NET Core terms at this level
Updated