Custom Service Dependency Injection In .Net Core 1.1

INTRODUCTION

There are various ways to inject dependency in MVC. Dependency injection is a technique to pass the required Dependency so that the desired action can be performed.

Dependency Injection In .Net Core

In this article we will go through with one of the key features of .Net Core, Custom Service dependency injection in .Net core 1.1, particularly at controller’s action level .Here are some important points which will help you to understand the facts easily in the following excerpts:

Dependency Injection

Dependency Injection (DI) is a pattern that shows how we can create loosely coupled classes. It means Class A is not directly dependent on class B and an object injects the dependency of another object. A dependency is an object that can be used as service, which is a part of this article.

Earlier in .Net there were some well-known dependency resolvers like Unity, Autofac and Ninject and so on.

  1. .Net Core allows you to inject dependency out-of-the-box, there is no need to add the reference of the above mentioned dependency resolver. As soon as you create a solution you will be able to configure services easily, .Net Core manages this operation at its level that indeed helps a developer to get rid of this matter.

  2. This article explains about how to configure custom service in .Net core and inject that service object in controller’s constructor as well as in action method. In the earlier versions of .Net it used to not inject the dependency at Action level. This is recently introduced with the release of .Net core. Action level injection of service makes an application lighter than controller level. Consider a scenario when you have a number of action methods defined in a single controller and you have injected the service at controller’s constructor. Though it’s being used at one action only, it looks like it’s not worth it.
PREREQUISITES
  • Visual Studio 2017 Enterprise/Professional
  • .Net Core 1.1

IMPLEMENTATION

In order to understand the dependency injection in .Net core 1.1, kindly refer to the given below image which states how the client application needs an object to access the implementation of given methods.
Dependency Injection In .Net Core

Now create a custom service class file as depicted below in screen shot:

Dependency Injection In .Net Core

FirstService class file has the following code snippet.

  1. using Microsoft.Extensions.Configuration;  
  2.   
  3. namespace DotnetPiper.Services  
  4. {  
  5.     public interface IFirstService  
  6.     {  
  7.         string WelcomeEquinox();  
  8.     }  
  9.     public class FirstService : IFirstService  
  10.     {  
  11.         public string welcomeEquinoxStr { get; set; }  
  12.         public FirstService()  
  13.         {  
  14.             welcomeEquinoxStr = "Welcome Folks in .Net Core presentation!"//Configuration["WelcomeEquinox"];  
  15.         }  
  16.         public string WelcomeEquinox()  
  17.         {  
  18.             return welcomeEquinoxStr;  
  19.         }  
  20.     }  
  21. }  

There is a startup class file which exists in solution and has two methods ConfigureServices and configure. Refer to the below image to know more about startup class file location under solution structure.

Dependency Injection In .Net Core

This method executes its functionality at runtime. Both operations have a different set of objectives as shown below,

ConfigureServices

This method gets called by the runtime. Use this method to add services to the container. And it takes one IServiceCollection object as an input. Refer to the given below screenshot.

Dependency Injection In .Net Core

Configure

This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

Dependency Injection In .Net Core

Register Your Custom Services

To register your custom application services in ConfigureService method kindly put an interface definition object type that will be requested from the container. The second type represents the concrete type (class) thaFit will be instantiated by the container.

  1. services.AddSingleton<IFirstService, FirstService>();  

 

If you notice in figure4, it has an IServiceCollection object which registers IFirstService object as singleton. Though there are two more ways to register your dependency in .Net core, those are AddScoped and AddTrsnsient as shown below,

  1. services.AddScoped<ArticlesService>();  
  2. services.AddTransient<ArticlesService>();  

Now create a product controller under controller’s folder and inject dependency in either of controller’s action, as shown below in screen shot,

Dependency Injection In .Net Core

Open Product Controller and inject IFirstService dependency in Details action method. Kindly refer to the below image for ref,

  1. public ActionResult Details([FromServices] IFirstService FirstService)  
  2.      {              
  3.          _FirstService = FirstService;  
  4.          return Content(_FirstService.WelcomeEquinox());  
  5.      }  

Note

[FromServices] attribute is newly introduced in .Net Core and brings an important role in order to inject abstract dependency at action method level.

Let’s run an application and see how it works.

Press F5 and application will open in browser just to ensure that application is up and running.

Dependency Injection In .Net Core

Kindly copy and paste the following url to ensure the IFirstService availability at action level.

http://localhost:64686/product/details

Dependency Injection In .Net Core

This is the final outcome which brings information through Custom service

Dependency Injection In .Net Core

Up Next
    Ebook Download
    View all
    Learn
    View all