IConfiguration vs IOptions NET
Synchronous and Asynchronous in .NET Core
Model Binding and Validation in ASP.NET Core
ControllerBase vs Controller in ASP.NET Core
ConfigureServices and Configure methods
IHostedService interface in .NET Core
ASP.NET Core request processing
| ASP.NET Core request processing | logging and monitoring in a .NET | |
Difference between IServiceCollection and IApplicationBuilder in .NET Core |
IServiceCollection and IApplicationBuilder are two core abstractions in ASP.NET Core, but they serve different purposes in the app lifecycle:
ConfigureServices or in Startup.cs or Program.cs or builder.Services). services.Add...() extensions.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddDbContext(options =>
options.UseSqlServer(Configuration.GetConnectionString("Default")));
services.AddScoped<IEmailService, EmailService>();
}
Configure in Startup.cs or app.Use... or Program.cs .NEt 6+ ). app.Use...(), app.Run(), app.Map...()
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
| Aspect | IServiceCollection 🛠 | IApplicationBuilder 🔀 |
|---|---|---|
| Stage | Startup: ConfigureServices | Startup: Configure |
| Purpose | Register dependencies (DI) | Build middleware pipeline |
| Focus | What services the app can use | How requests flow through the app |
| Methods | services.Add...() | app.Use...(), app.Map...() |
| Lifecycle | Runs before pipeline is built | Runs after services are registered |
| Example | services.AddDbContext<AppDbContext>() | app.UseRouting(); app.UseEndpoints(...) |
Summary:
| Aspect | IServiceCollection | IApplicationBuilder |
|---|---|---|
| Purpose | Registers services and dependencies into the DI container. | Configures the HTTP request pipeline by adding middleware components. |
| Used In | ConfigureServices(IServiceCollection services) method of Startup (or in Program.cs in minimal hosting). | Configure(IApplicationBuilder app, ...) method of Startup (or in Program.cs in minimal hosting). |
| When It Runs | During application startup, before the app begins handling requests. | Also during startup, but specifically when building the middleware pipeline that will process incoming requests. |
| Scope | Deals with what services are available to the app (e.g., logging, EF Core DbContext, authentication handlers). | Deals with how requests flow through middleware (e.g., routing, authentication, static files, exception handling). |
| Example Usage |
services.AddControllers(); services.AddDbContext<AppDbContext>(); services.AddAuthentication(); |
app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => endpoints.MapControllers()); |
| Lifecycle | Populates the IServiceProvider used throughout the app. | Consumes the IServiceProvider to resolve middleware dependencies and build the request delegate chain. |
| Analogy | Think of it as stocking the shelves with ingredients (services) before the restaurant opens. | Think of it as arranging the kitchen workflow so orders (requests) are processed in the right order. |
They work together:
IServiceCollection can be injected into middleware configured via IApplicationBuilder.For maintainability:
services.AddMyFeatureModule().app.UseMyFeatureModule() so each feature’s middleware is self-contained and easy to reorder. | ASP.NET Core request processing | logging and monitoring in a .NET | |