Interview Questions
Middleware is software that's assembled into an app pipeline to handle requests and responses. Each component: Chooses whether to pass the request to the next component in the pipeline. Can perform work before and after the next component in the pipeline.
ASP.NET Core supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies.
For more information specific to dependency injection within MVC controllers, see Dependency injection into controllers in ASP.NET Core.
Dependency injection (services) dependency injection
The Dependency Injection Design Pattern in C# is a process in which we are injecting the object of a class into a class that depends on that object. The Dependency Injection design pattern is the most commonly used design pattern nowadays to remove the dependencies between the objects.
ASP.NET Core includes a built-in dependency injection (DI) framework that makes configured services available throughout an app. For example, a logging component is a service.
ASP.NET Core uses dependency injection as a fundamental feature to manage dependencies throughout the framework. In order for the dependency injection framework to know how to resolve dependencies, these dependencies or “services” need to be configured first.
ASP.NET Core does this already for the very core services when you create the web host in your Program.cs but as you enable more features in your web application, you will need to add additional services to the application to opt into functionality.
For example services.AddMvc() adds the services required to enable the MVC functionality and middleware in the application. Or services.AddAuthentication() adds the services that are required to enable authentication in your application.
Since these functionalities are opt-in based and not enabled by default, the author of an application needs a way to control this. That is why the ConfigureServices method is there: Here, you can add the services you want to enable the functionality.
In addition, you can also use this to add your own services so that you can make use of dependency injection within the application as well; for example to resolve your own services within a controller.
Dependency injection is actually a rather complex topic, so I would suggest you to take a look at the documentation on dependency injection to see how it works and what you can do with it.