Make sure that the controller has a parameterless public constructor using Ninject

夙愿已清 提交于 2019-12-21 20:24:11

问题


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:

  • http://localhost:55555/api/Customer/
  • http://localhost:55555/api/Customer/8

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:

  • Make sure that the controller has a parameterless public constructor error
  • Make sure that the controller has a parameterless public constructor in Unity

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>();
        }        
    }
}

回答1:


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



来源:https://stackoverflow.com/questions/35834562/make-sure-that-the-controller-has-a-parameterless-public-constructor-using-ninje

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!