Dependency Injection By Using Ninject in Web API

Introduction

In this article, I will show you Dependency Injection in the Web API using Ninject.

The following is the procedure for creating the application.

Step 1

Create a Web API application as in the following:

  • Start Visual Studio 2012.
  • From the start window Select "Installed" -> "Visual C#" -> "Web".
  • Select "ASP.NET MVC4 Web Application" and click on the "Ok" button.

    Select Asp.Net Mvc4 web Application
  • From the "MVC4 Project" window select "Web API".

    Select Web API
  • Click on the "OK" button.

Step 2

Add an interface in the model folder.

  • In the "Solution Explorer".
  • Right-click on the "Model" -> "Add" -> "New Item".
  • Select "Installed" -> "Visual C#".
  • Select "Interface".

    Add Interface
  • Click on the "Add" button.

Add the following code:

  1. using System;    
  2. using System.Collections.Generic;    
  3. using System.Linq;    
  4. using System.Text;    
  5. using System.Threading.Tasks;    
  6. namespace ninjectAPI.Models    
  7. {    
  8.     public interface IDetail    
  9.     {    
  10.         string FullName { getset; }    
  11.     }    
  12. } 

Step 3

Create a Model Class using the following procedure:

  • In the "Solution Explorer".
  • Right-click on the Model Folder.
  • Select "Add" -> "Class".
  • Select "Installed" -> "Visual C#" and select class.

    Add Model Class
  • Click on the "Add" button

Add the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace ninjectAPI.Models  
  6. {  
  7.     public class Detail : IDetail  
  8.     {  
  9.         public string FullName  
  10.         {  
  11.             get  
  12.             {  
  13.                 return "Kamya Nehwal";  
  14.             }  
  15.             set  
  16.             {  
  17.                 this.FullName = value;  
  18.             }  
  19.         }  
  20.     }  
  21. } 

Step 4

Now install the Ninject.mvc3 package as in the following:

  • In the Tools menu.
  • Select "Library Package Manager" -> "Manage NuGet Packages for Solutions ".
  • In the search box type "ninject".

    Install Ninject.MVC3 Package
  • Install the Ninject.MVC3 package.
  • After installation select the "App_Start" folder; there is a "NinjectWebCommon.cs" class that exists.

    Display NinjectWebCommon class

Now select this class and perform some changes in the "RegisterServices()" method.

  1. private static void RegisterServices(IKernel kernel)  
  2. {  
  3.     kernel.Bind<IDetail>().To<Detail>();  
  4. } 

Step 5

Now add the "Ninject.WebApi.DependencyResolver" as in the following:

  • In the Tools menu.
  • Select "Library Package Manager" -> "Manage NuGet Packages for Solutions ".
  • In the search box type "Ninject.WebApi.DependencyResolver".

    Installed Package Ninject.WebApi.DependencyResolver
  • And install the "Ninject.WebApi.DependencyResolver" package.

After installing it, go to the "NinjectWebCommon.cs" file and add the following code in the CreateKernel() method.

  1. private static IKernel CreateKernel()  
  2. {  
  3.     var kernel = new StandardKernel();  
  4.     kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);  
  5.     kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();  
  6.     System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel);  
  7.     RegisterServices(kernel);  
  8.     return kernel;  
  9. } 

Step 6

Add a Controller class in the controller folder as in the following:

  • In the "Solution Explorer".
  • Right-click on the Controller folder.
  • Select "Add" -> "Controller".

    Add Controller
  • Click on the "Add" button.

Add the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Http;  
  6. using System.Web.Http;  
  7. using ninjectAPI.Models;  
  8. namespace ninjectAPI.Controllers  
  9. {  
  10.     public class ShowController : ApiController  
  11.     {  
  12.         private IDetail idetail = null;  
  13.        public ShowController(IDetail Idetail)  
  14.        {  
  15.            idetail = Idetail;  
  16.        }  
  17.        [HttpGet]  
  18.        public string GetValue()  
  19.        {  
  20.            return idetail.FullName;  
  21.        }   
  22.    }  
  23. }   

Step 7

Execute the application and change the URL to "http://localhost:13357/api/Show/GetValue".

 Output


Similar Articles