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


#1

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>();
services.AddTransient<IA,A>();
services.AddTransient<IB,B>();
…many more AddTransient
services.AddMvc();
}

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