ASP.NET Web API binding with ninject

27,108

Solution 1

I asked Brad Wilson about this and it has changed in MVC4 RC.

GlobalConfiguration.Configuration.ServiceResolver has been moved to GlobalConfiguration.Configuration.DependencyResolver

Use this implementation to create a Ninject DependencyResolver for your Web Api: https://gist.github.com/2417226

In NinjectWebCommon.cs:

// Register Dependencies
RegisterServices(kernel);

// Set Web API Resolver
GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);

Solution 2

This generic error message

Type 'Api.Controllers.ConsumerController' does not have a default constructor

can also occur if you do not make your constructor public, or the dependency cannot be resolved by the IoC container maybe because of a missing argument.

The error message is misleading to say the least.

Solution 3

You can install the NuGet package WebApiContrib.IoC.Ninject and add the following line of code to NinjectWebCommon.cs

GlobalConfiguration.Configuration.DependencyResolver = new NinjectResolver(kernel);

Solution 4

For someone landing here while searching '...does not have a default constructor' looking for an easy way to debug silently failing configurations:

  1. Remove constructor-injected objects until the constructor can be invoked
  2. For each of the previously injected, now uninitialized objects, invoke using:

ServiceLocator.Current.GetInstance<[OBJ-TYPE]>()

The offending object will cause an ActivationException with a descriptive Message. You'll have something to go for.

Remember to remove call to ServiceLocator post-fix. Ie. this is not a recommendation to use the service locator anti pattern outside debugging.

Share:
27,108
Diver Dan
Author by

Diver Dan

Updated on February 25, 2020

Comments

  • Diver Dan
    Diver Dan about 4 years

    I have just installed the mvc4 rc update and I am trying to build an api application with little luck.

    I am using ninject but cant get my controllers to load. I keep getting an error

    Type 'Api.Controllers.ConsumerController' does not have a default constructor

    I am very new to mvc and using injection so please bear with me.

    I havent done anything special to the default binding that is created via nuget

     public static class NinjectWebCommon 
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();
    
        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start() 
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }
    
        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }
    
        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
    
            RegisterServices(kernel);
            return kernel;
        }
    
        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<IConsumerRepository>().To<ConsumerRepository>();
        }        
    }
    

    My controller looks like

       private readonly IConsumerRepository _repository;
    
        public ConsumerController(IConsumerRepository repository)
        {
            _repository = repository;
        }
    
        [HttpGet]
        public IQueryable<Consumer> Get(Guid id)
        {
            return _repository.Get(id).AsQueryable();
        }
    

    What do I need to do to get the api controllers to work with ninject?

    Sorry if this is simple stuff

    I tried your suggestion Michael however after changing the the webcommon.cs to this

      private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
    
            RegisterServices(kernel);
            GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
            return kernel;
        }
    
        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<IConsumerRepository>().To<ConsumerRepository>();
        }
    

    I get an error when

    var kernel = new StandardKernel();
    

    is called

    Method 'GetFilters' in type 'Ninject.Web.WebApi.Filter.DefaultFilterProvider' from assembly 'Ninject.Web.WebApi, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7' does not have an implementation.

    What am I missing?

  • Diver Dan
    Diver Dan almost 12 years
    Hi Michael when I use your suggestion I get an error Method 'GetFilters' in type 'Ninject.Web.WebApi.Filter.DefaultFilterProvider' from assembly 'Ninject.Web.WebApi, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7' does not have an implementation.
  • Diver Dan
    Diver Dan almost 12 years
    I removed the reference to webapi and it looks to have resolved it.
  • Michael Baird
    Michael Baird almost 12 years
    I think you're using a Ninject plugin that is meant for an older version of WebApi. Remove that reference and only use Nuget packages Ninject, Ninject.Web.Common and Ninject.MVC3 (Still works for MVC4)
  • bradjive
    bradjive almost 12 years
    I was experiencing the same problem and removed the ninject's web.api reference just like you said. Now it works!
  • Rn222
    Rn222 over 11 years
    I tried this but I'm still getting the "No parameterless constructor defined for this object." error. I also just upgraded from MVC 3 so maybe I missed something else...
  • David
    David over 11 years
    I had to follow the steps in strathweb.com/2012/05/…, which includes the step in this answer.
  • VahidNaderi
    VahidNaderi over 10 years
    So simple but the private constructor was my problem and I was checking everything but I couldn't see it, your hint saved me another day.
  • Jesus is Lord
    Jesus is Lord over 10 years
    I had a constructor which requested an interface, which was bound to a class that requested another interface - that interface didn't have a binding and instead of letting me know that - it just said that the ApiController didn't have a default constructor - double check that your dependencies are all registered if you get this.
  • eddievan
    eddievan about 9 years
    I have been searching for this answer for so long, great work! I am surprised they don't update the nuget package to include this....
  • Wes Duff
    Wes Duff almost 8 years
    This worked for me! Thanks... Also make sure to set your bindings in your NinjectWebCommon.cs file :)