ASP.NET - What is the difference between ConfigureServices and Configure?

In previous versions of .Net the standard was to have a startup.cs file which implemented two methods:

  • ConfigureServices: Which is responsible for configuring dependency injection. Whenever a controller endpoint is hit the controller may have dependencies that must be resolved. In ASP.NET you will see the AddTransient, AddSingleton or AddScoped methods are used to setup and resolve these dependencies. A dependency could be any service, a common one would be database, logging or any sort of communication.
  • Configure: which is used to set up any ASP.NET middleware or anything that has to do with routing or handling of HTTP requests. A common part of middleware is authentication or request logging.

You will notice that most ConfigureServices calls add to a dependency injection container and most configure use some sort of middleware. You might be wondering which one is called first, the ConfigureServices (dependency injection) is called first and then Configure (HTTP setup). Configure and ConfigureServices methods from a startup.cs class could look like the following:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
    services.AddEndpointsApiExplorer();
    services.AddSwaggerGen();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (app.Environment.IsDevelopment())
    {
        app.UseSwagger();
        app.UseSwaggerUI();
    }

    app.UseHttpsRedirection();
    app.UseAuthorization();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}

Next we will see how this look in never versions.

Without a startup.cs class

From .Net 6, a standard ASP.NET application has the two responsibilities within the same scope in the program.cs class and you would see the following when creating a new ASP.NET application:

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

In the above we have two comments. // Add services to the container which is the equal to the ConfigureServices call and // Configure the HTTP request pipeline which is equal to the Configure call. First is the setup of dependency injection and the second is the setup of handling HTTP requests.

That is all

I hope you found this useful, please leave a comment down below if you liked this or if I missed something!