ConfigureServices and Configure methods are the two methods inside the Startup class of an ASP.NET Core web application.
These methods take different arguments and perform different functionalities.
These methods are used to register services and middleware components to the .NET Core Dependency Injection framework.
In a .NET 6 minimal application, where there is no Startup class involved, you can omit these methods and use the WebApplication object to register services and middlewares directly.
What is a ConfigureServices() method
You will use ConfigureServices method to register services in the ASP.NET Core Dependency Injection container. You can register the default built-in services available in .NET or you can create and register custom classes as services.
The following code snippet shows a simple Startup class with ConfigureServices method in a .NET 6 application.
using Microsoft.Extensions.DependencyInjection;
namespace MyWebApplication;
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<ITransientService, TransientService>();
services.AddScoped<IScopedService, ScopedService>();
services.AddSingleton<ISingletonService, SingletonService>();
services.Configure<MyOptions>(Configuration.GetSection("MyOptions"));
}
}
What is a Configure() method
You will use Configure() method to add middleware components to the ASP.NET Core request pipeline. The order in which these are added in this method decides the order in which they are executed for an incoming request.
The following code snippet shows a simple Startup class with default Configure method in a .NET 6 application.
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace MyWebApplication;
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
// Configure services here
}
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.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
What are the differences between ConfigureServices and Configure methods?
ConfigureServices | Configure |
---|---|
to register services in the Dependency Injection Container | to configure middleware components in the request pipeline |
single parameter of type IServiceCollection | a required parameter of type IApplicationBuilder with possible parameters of any Service which is registered in the ConfigureServices() method |
an application must contain a definition for ConfigureServices | Configure method definition is optional if there are no middlewares to be added to the pipeline |
The following code snippet shows a .NET 6 Minimal API, that omits the Startup class and configures Services and Middleware components within the same place.
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
var builder = WebApplication.CreateBuilder();
// ConfigureServices: Add services to the container
builder.Services.AddSingleton<ISomeService, SomeService>();
var app = builder.Build();
// Configure: Set up the request handling pipeline
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.MapGet("/", (ISomeService myService) => {
return "Hello, " + myService.GetMessage();
});
app.Run();