| SQL-and-NoSQL-database :👈 | 👉:IHostedService interface in .NET Core |
ConfigureServices and Configure methods in the Startup class of NET Core |
ASP.NET Core Startup Class
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.
1️⃣ ConfigureServices(IServiceCollection services)
Purpose: To register application services and configure Dependency Injection (DI).
What happens here:
- You add framework services (e.g., MVC, Razor Pages, Identity, EF Core).
- You register your own application services (e.g., repositories, business logic classes).
- You configure options and bind configuration sections.
Key points:
- Runs before Configure.
- Populates the DI container so services can be injected into controllers, middleware, etc.
- Uses
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"));
}
2️⃣ Configure(IApplicationBuilder app, IWebHostEnvironment env)
Purpose: To define the HTTP request pipeline — i.e., the sequence of middleware that processes incoming requests and outgoing responses.
What happens here:
- You add middleware components that handle requests (e.g., authentication, routing, static files).
- You decide the order in which middleware runs.
- You can branch the pipeline based on environment (Development, Production).
Key points:
- Runs after ConfigureServices.
- Uses
app.Use...,app.Run..., andapp.Map...methods. - Middleware order is critical — it affects how requests are processed.
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?}");
});
}
🔍 How They Work Together
| 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:
- ConfigureServices = "What tools and helpers will my app have?"
- Configure = "In what order will my app use those tools to process a request?"
💡 Pro Tip
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.
| SQL-and-NoSQL-database :👈 | 👉:IHostedService interface in .NET Core |