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
| Kestrel In .NET Core | IHostedService interface in .NET Core | |
ConfigureServices and Configure methods in the Startup class of NET Core |
In ASP.NET Core, the Startup class is where you define how your application is built and how it will handle requests. The two key methods — ConfigureServices and Configure — serve different but complementary purposes in the app’s startup lifecycle.
Purpose: To register application services and configure Dependency Injection (DI).
What happens here:
Key points:
services.Add... methods.Example:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services
services.AddControllersWithViews();
// Configure EF Core
services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
// Register custom services
services.AddScoped<IEmailService, EmailService>();
// Configure options
services.Configure<MySettings>(Configuration.GetSection("MySettings"));
}
Purpose: To define the HTTP request pipeline — i.e., the sequence of middleware that processes incoming requests and outgoing responses.
What happens here:
Key points:
app.Use..., app.Run..., and app.Map... methods.Example:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
| Step | Method | Purpose |
|---|---|---|
| 1 | ConfigureServices | Register all services and dependencies your app needs. |
| 2 | Configure | Build the middleware pipeline that uses those services to handle requests. |
Think of it like this:
In .NET 6+ minimal hosting model, these two methods are often replaced by calls on the builder.Services and app objects in Program.cs, but the concepts are exactly the same — service registration first, pipeline configuration second.
| Kestrel In .NET Core | IHostedService interface in .NET Core | |