What is the purpose of adding services.AddMvc() in the ConfigureServices method in mvc 6?

11,543

In this new ASP.NET 5 world there are two primary aspects of app development.

  1. Dependency Injection. Aka what services are going to be required to run our application?
  2. The application/request pipeline. Essentially the way we answer the question of "What to do when a request hits the server".

Due to these two primary concerns there then happens to be two mechanisms for tying into the system.

First, UseMVC is the way your application can say I want MVC to take a part in the request handling stage at "this" point. It's essentially a shortcut to an MVC specific middleware.

Second, AddMvc is the way your application says that you want the MVC services available to the system (needed in order for UseMvc) to work correctly. Therefore, if you were to try and do UseMvc without adding the corresponding MVC services the call would throw. Note that this adds the appropriate MVC services to the DI container.

Hopefully this answered your questions, for more information on it you can check out http://www.asp.net/vnext for more general information. For something more specific/video I did a talk a while back at Orchard conference where I go over several of the core pieces https://www.youtube.com/watch?v=kqgIByKn9Wk

Note: I gave the talk a while back, some concepts are outdated/may have changed but the core concepts are the same.

Share:
11,543

Related videos on Youtube

user2070369
Author by

user2070369

Updated on October 06, 2022

Comments

  • user2070369
    user2070369 over 1 year

    Why is not enought to just add app.UseMvc() in the Configuration method in a mvc6 application? why it is also necessary to add the services.AddMvc() in the ConfigureServices method? and where can I find more info about this?

    Thank you.

  • user2070369
    user2070369 about 9 years
    Good answer, but for example, NancyFX doesn't need the add anything to the ConfigureService method, just the useNancy in the Configure method, that is what is confusing me.
  • N. Taylor Mullen
    N. Taylor Mullen about 9 years
    Ultimately it's up to the framework for how they'd like to incorporate their "add me" calls. As you'll see in the video I end up doing app.UseServices(services => ....) in order to register services. This was a quick way to register services for a demo. The better approach is to use the ConfigureServices method in your startup class. We decided to keep the two separated since they're truly two different concepts, having the two separate enables you to put all your DI code in one place and your Use code in another.
  • Janus007
    Janus007 over 8 years
    @user2070369 Not quite correct, NancyFX does require you to override GetApplicationContainer if you want to a DI-framework that isn't wired up inside a NancyBootstrapper. So for example in your ASP.NET 5 - Configure-function where you put UseNancy you would also need to build a DI container first and then return that from GetApplicationContainer
  • Alexander Derck
    Alexander Derck over 5 years
    Good answer but I'm missing 2 things: 1) By using UseMvc, why and how does it need the request pipeline? I'd say routing (wiring up controller actions to http endpoints) and building the response object, not sure if there's anything else. 2) By using AddMvc, which MVC services get added? Perhaps anyone has a few examples? I can't really find any documentation about it, but probably the engine to parse the views etc?