0

I am trying to develop an application, through a book, in WebApi. It is a simple application but whenever I am testing this application using fiddler, it is throwing error - {"Message":"An error occurred when trying to create a controller of type 'TasksController'. Make sure that the controller has a parameterless public constructor."}. I am using ninject for dependency injection. Here is some of my code:

public class NinjectConfigurator
{
    public void Configure(IKernel container)
    {
        AddBindings(container);
    }

    private void AddBindings(IKernel container)
    {
        ConfigureLog4net(container);
        ConfigureNHibernate(container);
        ConfigureUserSession(container);
        ConfigureAutoMapper(container);

        container.Bind<IDateTime>().To<DateTimeAdapter>().InSingletonScope();
        container.Bind<IAddTaskQueryProcessor>().To<IAddTaskQueryProcessor>().InRequestScope();
        container.Bind<IAddTaskMaintenanceProcessor>().To<AddTaskMaintenanceProcessor>().InRequestScope();
    }

}

public class TasksController : ApiController
{
    private readonly IAddTaskMaintenanceProcessor _addTaskMaintenanceProcessor;

    //public TasksController() { }
    public TasksController(IAddTaskMaintenanceProcessor addTaskMaintenanceProcessor)
    {
        _addTaskMaintenanceProcessor = addTaskMaintenanceProcessor;
    }

    [Route("", Name="AddTaskRoute")]
    [HttpPost]
    public Task AddTask(HttpRequestMessage requestMessage, NewTask newTask)
    {
        var task = _addTaskMaintenanceProcessor.AddTask(newTask);

        return task;
    }
}

I am not sure why I am getting the error. I am following all the instructions of the book. Please help!!

user1812749
  • 67
  • 1
  • 2
  • did you check whether that IAddTaskMaintenanceProcessor implementation has public constructor and you didn't forget to register all of it's required interfaces in IoC container? – Denis Dec 24 '15 at 19:37
  • Yes, IAddTaskMaintenanceProcessor implementation has public constructor. I have placed the code in the first block which shows that I am registering the interfaces in IoC container. – user1812749 Dec 24 '15 at 19:42
  • public class AddTaskMaintenanceProcessor : IAddTaskMaintenanceProcessor { private readonly IAutoMapper _autoMapper; private readonly IAddTaskQueryProcessor _queryProcessor; public AddTaskMaintenanceProcessor(IAddTaskQueryProcessor queryProcessor, IAutoMapper autoMapper) { _queryProcessor = queryProcessor; _autoMapper = autoMapper; } } – user1812749 Dec 24 '15 at 19:44
  • Check the similar question at http://stackoverflow.com/a/10592456/538387 .Hope this will help you. – Saketh Dec 24 '15 at 19:46
  • and make sure IAddTaskQueryProcessor implementation has public constructor too – Denis Dec 24 '15 at 19:51
  • Thank you very much Denis for the direction. The issue is gone. The code - container.Bind().To() is binding interface with interface and that was the issue.When I did bind the interface with the implementation then the problem was gone. The new code is container.Bind().To(). Again thank you for the help. It was a typo. – user1812749 Dec 24 '15 at 19:53
  • Are you already telling the ASP.NET about the container? for example via `DependencyResolver`? – Yacoub Massad Dec 24 '15 at 19:55

1 Answers1

0

The code - container.Bind().To() is binding interface with interface and that was the issue.When I did bind the interface with the implementation then the problem was gone. The new code is container.Bind().To(). Again thank you for the help. It was a typo.

user1812749
  • 67
  • 1
  • 2