Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ninject for Asp.net Web API

I got this error when using Ninject with Web API, but it works with MVC Controller:

Type 'App.Web.Controllers.ProductController' does not have a default constructor

NinjectControllerFactory :

public class NinjectControllerFactory : DefaultControllerFactory
    {
        private IKernel ninjectKernel;

        public NinjectControllerFactory()
        {
            ninjectKernel = new StandardKernel();
            AddBindings();
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType);
        }

        public void AddBindings()
        {
            ninjectKernel.Bind<IProductRepository>().To<EFProductRepository>();
        }
    }

Global.asax.cs :

BundleConfig.RegisterBundles(BundleTable.Bundles);

            ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());

ProductController :

   public class ProductController : ApiController
        {
            private IProductRepository repository;

            public ProductController(IProductRepository ProducteRepository)
            {
                this.repository = ProductRepository;
            }

            public IEnumerable<Product> GetAllProducts()
            {
                return repository.Products.AsEnumerable();
            }
        }
like image 341
Alvin Avatar asked Jul 20 '13 09:07

Alvin


People also ask

What is ninject Web API?

NInject is a popular IOC container that can be used to inject dependencies in your WebAPI controllers easily. IDG. Dependency injection is a software design pattern that helps you to build pluggable implementations in your application using loosely coupled, testable components.

Where do I register dependency injection in Web API?

Set the dependency resolver on the DependencyResolver property of the global HttpConfiguration object. The following code registers the IProductRepository interface with Unity and then creates a UnityResolver . public static void Register(HttpConfiguration config) { var container = new UnityContainer(); container.

What is ninject used for?

Ninject is a lightweight dependency injection framework for . NET applications. It helps you split your application into a collection of loosely-coupled, highly-cohesive pieces, and then glue them back together in a flexible manner.

Which of the following API is used to perform dependency injection?

Here we will use Ninject for dependency injection. The following is our sample Web API that uses instance of a class that implements IRepository. The following are IRepository and StudentRepository class.


2 Answers

You have overriden the DefaultControllerFactory. But this is used to instantiate ASP.NET MVC controllers (one deriving from System.Web.Mvc.Controller). It has strictly nothing to do with ASP.NET Web API controllers (the ones deriving from System.Web.Http.ApiController).

So basically what you have done here is dependency injection into ASP.NET MVC. If you want to use this for the Web API you may take a look at the following guides:

  • http://www.strathweb.com/2012/05/using-ninject-with-the-latest-asp-net-web-api-source/
  • http://www.peterprovost.org/blog/2012/06/19/adding-ninject-to-web-api/
like image 136
Darin Dimitrov Avatar answered Sep 17 '22 02:09

Darin Dimitrov


You should use the latest Ninject Web API package, which solves these problems already. See here: http://nuget.org/packages/Ninject.Web.WebApi.WebHost/

like image 43
Teoman Soygul Avatar answered Sep 17 '22 02:09

Teoman Soygul