What are the differences between ConfigureServices() and Configure() in ASP.NET Core?

In a nutshell:

ConfigureServices is used to configure Dependency Injection

public void ConfigureServices(IServiceCollection services)
{
    // register MVC services
    services.AddMvc();

    // register configuration
    services.Configure<AppConfiguration>(Configuration.GetSection("RestCalls")); 

    // register custom services
    services.AddScoped<IUserService, UserService>();
    ...
}

Configure is used to set up middlewares, routing rules, etc

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    // configure middlewares
    app.UseMiddleware<RequestResponseLoggingMiddleware>();
    app.UseMiddleware<ExceptionHandleMiddleware>();

    app.UseStaticFiles();

    // setup routing
    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "Default",
            template: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = 1 });

    });
}

Read ASP.NET Core fundamentals to understand how it works in details.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)