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 inan easier way, we will create a sample application going step-by-step. My example service will provide GUID to the respective controller. So, let's start quickly. 
  1. Create a new project - Create a new project using ASP.NET Web Application template as shown below:



  2. Add Service Interface - Create an interface named IGUIDService under Services folder as shown below:
    1. public interface IGUIDService  
    2. {  
    3.     string GenerateGUID();  
    4. }  
  3. Add Service Implementation - Create a class named GUIDServiceunder Services folder and provide the implementation of IGUIDService interface as shown below:
    1. public class GUIDService : IGUIDService  
    2. {  
    3.     public string GenerateGUID()  
    4.     {  
    5.         return ("Generated GUID is: " + Guid.NewGuid()).ToString();  
    6.     }  
    7. }  
  4. Add a Controller - Next task is to add a Controller namedGUIDController by right clicking on Controllers folder as shown below:

    MVC Controller
                                               
  5. Add a View - Before adding a View, create a folder named under Views folder. Now add a View by right clicking on GUID folder as shown:

    MVC View Page                                                                                                                                                                                                                                                       Update the code inside View as shown below:     
                                                                                                                               
  6. Use Service in Controller - Now instantiate the service and set the data for View as shown below:
    1. public class GUIDController : Controller  
    2. {  
    3.     private IGUIDService _guidService;  

    4.     public GUIDController(IGUIDService guidService)  
    5.     {  
    6.         _guidService = guidService;  
    7.     }  
    8.     public IActionResult Index()  
    9.     {  
    10.         ViewData["GeneratedGUID"] = _guidService.GenerateGUID();  
    11.         return View();  
    12.     }  
    13. }  
  7. Register Service - Last step is to register the service in the ConfigureServices method of Startup class as shown below:
    1. public void ConfigureServices(IServiceCollection services)  
    2. {  
    3.     ...  
    4.     ...  
    5.     services.AddMvc();             
    6.     services.AddInstance<IGUIDService>(new GUIDService());  
    7. }  
Everything is set. Now run your application and you would be able to see GUID on browser as:
Hope you got an idea on how to inject services in ASP.NET Core 1.0.

Up Next
    Ebook Download
    View all
    Learn
    View all