3

Here is the issue at hand:

While calling my CustomerController through the URL, I get the following exception:

ExceptionMessage:

An error occurred when trying to create a controller of type 'CustomerController'. Make sure that the controller has a parameterless public constructor.

I am using the following url's:

Please note: The /api/Customer/ call were working before I refactored the logic into a business class and implemented dependency injection.

My research suggests that I am not registering my interface and class correctly with Ninject, but not sure what step I am missing.

Researched Links:

Here is my question What is causing this exception? I am registering my interface/class within Ninject, but it doesn't seem to recognize the mapping correctly. Any thoughts?

Customer Controller

public class CustomerController : ApiController
{
    private readonly ICustomerBusiness _customerBusiness;

    public CustomerController(ICustomerBusiness customerBusiness)
    {
        _customerBusiness = customerBusiness;
    }

    // GET api/Customer
    [HttpGet]
    public IEnumerable<Customer> GetCustomers()
    {
        return _customerBusiness.GetCustomers();
    }

    // GET api/Customer/Id
    [HttpGet]
    public IEnumerable<Customer> GetCustomersById(int customerId)
    {
        return _customerBusiness.GetCustomerById(customerId);
    }
}

Customer Business

public class CustomerBusiness : ICustomerBusiness
{
    private readonly DatabaseContext _databaseContext = new DatabaseContext();

    public IEnumerable<Customer> GetCustomers()
    {
        return _databaseContext.Customers;
    }

    public IQueryable<Customer> GetCustomerById(int customerId)
    {
        return _databaseContext.Customers.Where(c => c.CustomerId == customerId);
    }       
}

Customer Business Interface

public interface ICustomerBusiness
{
    IQueryable<Customer> GetCustomerById(int customerId);
    IEnumerable<Customer> GetCustomers();
}

NinjectWebCommon

using System;
using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using MyReservation.API;
using MyReservation.API.Business;
using Ninject;
using Ninject.Web.Common;

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(NinjectWebCommon), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(NinjectWebCommon), "Stop")]

namespace MyReservation.API
{
    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();
            try
            {
                kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
                kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

                RegisterServices(kernel);
                return kernel;
            }
            catch
            {
                kernel.Dispose();
                throw;
            }
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<ICustomerBusiness>().To<CustomerBusiness>();
        }        
    }
}
Community
  • 1
  • 1
ChaseHardin
  • 2,189
  • 7
  • 29
  • 50
  • You need to inject ICustomerBusiness to your CustomerController. A hint on how to do this you can find here: http://stackoverflow.com/questions/2227548/creating-an-instance-using-ninject-with-additional-parameters-in-the-constructor – MarkusE Mar 07 '16 at 04:38
  • Possible duplicate of [Ninject "No parameterless constructor defined for this object."](http://stackoverflow.com/questions/20127901/ninject-no-parameterless-constructor-defined-for-this-object) – BatteryBackupUnit Mar 07 '16 at 12:33
  • This / similar questions have been asked plenty of times. The two questions you list under "Researched links" are both for unity. Unity may have a different behavior so it may not apply. Your issue is that you haven't set up your project properly to use ninject - the error your receiving stems from the default dependency resolver - ninject will *never* emit that exception message. – BatteryBackupUnit Mar 07 '16 at 12:35
  • you should also mention which version of asp.net MVC / webapi (if relevant) you're using - and add the appropriate tags to your question, too. – BatteryBackupUnit Mar 07 '16 at 12:36
  • @MarkusE. thanks for the comment, but I'm unsure what you mean by injecting `ICustomerBusiness` into the `CustomerController`. I thought this was being done in the `CustomerController` constructor. Any other hints? – ChaseHardin Mar 07 '16 at 15:52
  • @BatteryBackupUnit I know that there are many similar questions online, but I was not able to resolve my issue after reading through those posts... I also checked the resource link that you provided and I've already implemented the answer that they suggest. – ChaseHardin Mar 07 '16 at 15:56
  • @CodeChaser you've not been the first to say the exact same thing (have seen them, tried that) who's been proved wrong and later the questions have been deleted voluntarily / closed as *exact duplicate*. From the exception message i can tell you that ninject is not setup correctly. The ninject dependency resolver is not being used - otherwise a `NinjectActivationException` would occur if binding could not be resolved (what you refert to as incorrect "registration"). – BatteryBackupUnit Mar 07 '16 at 16:16
  • So i strongly suggest you double-check and if you still cannot fix it then at least provide *more / other* info than the other questions, because if the code you're showing us is exactly the same as with other issues then it seems the issues cannot be discerned from *that* code only. Also, you're still missing the info which version of what you're employing (asp.net mvc, web api...) – BatteryBackupUnit Mar 07 '16 at 16:18

1 Answers1

9

For the same problem I installed the nuget packages

  • ninject

  • ninject.web.common

  • ninject.web.common.webhost

  • ninject.web.webapi

  • ninject.web.webapi.webhost

    and worked

Community
  • 1
  • 1