Here is the problem:
When calling my CustomerController through a URL I get the following exception:
ExceptionMessage:
An error occurred while trying to create a controller such as' CustomerController. Make sure the controller has an unsigned public constructor.
I use the following URLs:
Note: The call /api/Customer/ worked before I reorganized the logic into a business class and implemented dependency injection.
My research shows that I am not correctly registering my interface and class with Ninject , but I donβt know which step I am missing.
Links investigated:
- Make sure the controller has an error without a constructor
- Make sure the controller has an immortal public constructor in Unity
Here is my question. What causes this exception? I am registering my interface / class inside Ninject , but it looks like it does not correctly recognize the mapping. Any suggestions?
Client controller
public class CustomerController : ApiController { private readonly ICustomerBusiness _customerBusiness; public CustomerController(ICustomerBusiness customerBusiness) { _customerBusiness = customerBusiness; }
Business for customers
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();
c # dependency-injection ninject
ChaseHardin Mar 07 '16 at 0:55 2016-03-07 00:55
source share