ASP.NET Core Interview Question #14

What is IServiceProvider?

ASP.NET Core Fundamentals Junior Beginner

Quick Interview Answer

IServiceProvider is the runtime service resolver built from the application's service registrations. ASP.NET Core uses it internally to create controllers and resolve registered dependencies.

Detailed Explanation

IServiceProvider is the runtime service resolver built from the application's service registrations. ASP.NET Core uses it internally to create controllers and resolve registered dependencies.

Application code normally prefers constructor injection instead of manually calling GetService or GetRequiredService. Direct resolution is appropriate in limited cases such as creating a scope during startup or in infrastructure code.

Code Example

using var scope = app.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();