Avatar ·

Difference between AddTransient, AddScoped and AddSingleton in ASP.NET Core: when and what to use?

Hi everyone! I recently started learning ASP.NET Core and the built-in dependency injection (DI) mechanism. I think I understand how to register services in `ConfigureServices`, but the choice of service lifetime confuses me. I have a simple example. Let's say I'm writing an online store application, and I have a `CartService` that manages the user's cart, and a `LoggerService` that logs actions. In the startup, I register them like this: ```csharp public void ConfigureServices(IServiceCollection services) { services.AddTransient(); services.AddScoped(); services.AddSingleton(); } ``` I don't fully understand the difference between `AddTransient`, `AddScoped` and `AddSingleton`. There are many explanations on the internet, but they