Tuesday, April 3, 2018

Configure vs ConfigureServices in ASP.NET Core 2.0


Both words sound similar but there is a very thin line which differentiates these two. Let’s have a look at key differences between these two methods.


Configure
ConfigureServices
Main purpose
Does all the initial setup
Does service configurations
Default Location
Resides in Startup.cs
Resides in Startup.cs
Execution sequence
Hits prior to ConfigureServices
Hits after Configure
Ordering
Order of steps mentioned in this method matters
Order of the statements written to add services doesn’t matter
HTTP request
HTTP request first hits in this method
                                 -
Parameters
Configure(IApplicationBuilder app, IHostingEnvironment env)
ConfigureServices(IServiceCollection services)
Middleware
Setting up all the middleware are done in this method
                                 -
Services
 -           
All the required services are added in this method

Note: All the services which are added to IServiceCollection can be utilized across the application.

2 comments:

  1. Really Nice Article. Does every request hit configure method in startup? Actually I places a breakpoint on Configure while sending the request. It is hitting the controller but not Configure method.

    ReplyDelete
    Replies
    1. When you will launch your application, first ConfigureServices(...) will hit and then Configure(...). After that based on actions, controller will it.

      Delete