ASP.NET Core Interview Question #53
What is IServiceCollection?
Dependency Injection & Services Junior Beginner
Quick Interview Answer
IServiceCollection is the collection used to register services with ASP.NET Core's dependency injection container before the application is built.
Detailed Explanation
IServiceCollection is the collection used to register services with ASP.NET Core's dependency injection container before the application is built.
Each registration describes a service type, its implementation or factory, and its lifetime. Framework extension methods such as AddControllersWithViews(), AddDbContext(), AddAuthentication(), and AddHttpClient() also add registrations to IServiceCollection.
In modern ASP.NET Core, it is normally accessed through builder.Services in Program.cs.
Code Example
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
builder.Services.AddScoped<IProductRepository, ProductRepository>();
builder.Services.AddScoped<IProductService, ProductService>();