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:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

 

namespace ninjectAPI.Models

{

    public interface IDetail

    {

        string FullName { get; set; }

      

    }

  

}

 

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:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

 

namespace ninjectAPI.Models

{

    public class Detail : IDetail

    {

        public string FullName

        {

            get

            {

                return "Kamya Nehwal";

            }

            set

            {

                this.FullName = value;

            }

        }

    }

}

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.

private static void RegisterServices(IKernel kernel)

        {

            kernel.Bind<IDetail>().To<Detail>();

        }    

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.

private static IKernel CreateKernel()

        {

            var kernel = new StandardKernel();

            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);

            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

            System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel);

 

            RegisterServices(kernel);

            return kernel;

        }

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:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Net.Http;

using System.Web.Http;

using ninjectAPI.Models;

namespace ninjectAPI.Controllers

{

    public class ShowController : ApiController

    {

        private IDetail idetail = null;

       public ShowController(IDetail Idetail)

       {

           idetail = Idetail;

       }

       [HttpGet]

       public string GetValue()

       {

           return idetail.FullName;             

       }   

   }

    }

 

Step 7


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

 

 Output

 

Next Recommended Readings