Introduction

In this article, we will see the difference between AddScoped vs AddTransient vs AddSingleton in .net core.

Why we require

Three types of lifetime and registration options

  1. Scoped
  2. Transient
  3. Singleton

Scoped

services.AddScoped<IAuthService,AuthService>();

Transient

services.AddTransient<ICronJobService,CronJobService>();

Singleton

services.AddSingleton<ILoggingService, LoggingService>();

When to use which Service

Singleton approach => We can use this for logging service, feature flag(to on and off module while deployment), and email service

Scoped approach => This is a better option when you want to maintain a state within a request.

Transient approach => Use this approach for the lightweight service with little or no state.

What do you think?

I hope you enjoyed this article. I would like to have feedback from my readers.

Your valuable feedback, questions, or comments about this article are always welcome.