Skip to main content

Posts

Showing posts with the label Controller

Consuming Services in ASP.NET Core MVC Controller

Another interesting feature of ASP.NET Core is service consumption using Dependency Injection. I already provided the overview of Dependency Injection in my earlier articles. Moving further, in this article we will see how one can inject and consume services to controller using dependency injection. In ASP.NET, you can access services at any stage of your HTTP pipeline through dependency injection. By default, it provides Constructor injection, but it can easily be replaced by any other container of your choice. Before moving ahead, one point is very important to understand and that is lifetime of services. ASP.NET Core allows you to configure four different lifetimes for your services. Transient - Created on every request Scoped - Created once per request Singleton - Created first time when they are requested Instance - Created in ConfigureServices method and behaves like a Singleton To understand the service consumption in easier way, we will create a sample applica