Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 693
user2070369 Avatar asked Feb 20 '15 20:02

user2070369


1 Answers

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.

like image 174
N. Taylor Mullen Avatar answered Oct 11 '22 03:10

N. Taylor Mullen