Friday, May 5, 2023

What is the difference between services.AddTransient & service.AddScoped & service.AddSingleton methods are Asp.Net Core?

 Transient objects are created for every request (when requested).

 The service can be added as Transient using AddTransient method of IServiceCollection.

 This lifetime can be used in stateless service and it is a way to add lightweight service.

Scoped objects are the same within a request, but different across different requests.

 ASP.NET Core will create and share an instance of the service per request to the application.


It will create a new instance in the new request.

he service can be added as scoped using an AddScoped method of IServiceCollection.

Singleton objects created the first time they're requested (or when ConfigureServices is run and an instance is specified with the service registration).