Configure vs ConfigureServices – Explained in a Simple Way

What is ConfigureServices and Configure methods in ASP.NET Core. What are the major differences between these two methods with examples.

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?

ConfigureServicesConfigure
to register services in the Dependency Injection Containerto configure middleware components in the request pipeline
single parameter of type IServiceCollectiona 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 ConfigureServicesConfigure 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();

Buy Me A Coffee

Found this article helpful? Please consider supporting!

Ram
Ram

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. You can connect with me on Medium, Twitter or LinkedIn.

Leave a Reply

Your email address will not be published. Required fields are marked *