Why we need to Transient Service in StartUp.cs in .net core?


Why we need to add Transient of Services in Startup.cs file like below

public void ConfigureServices(IServiceCollection services)
services.AddTransient<ITopicAreaService, TopicAreaService>();
services.AddTransient<ICountryService, CountryService>();
…many more AddTransient

It is said that every time the service is resolved from a DI container, a new instance is created for all the Services. If this is done then unnecessary memory allocation is done for all Services Is it good to do so. Please give me a reason. Why we are doing so ?

.NET Foundation Website | Blog | Projects | Code of Conduct