0

I have a simple MVC 4 Application:

I installed Ninject MVC 3 from nuget and I register the services into NinjectWebCommon.cs from App_Start folder:

/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
    kernel.Bind<IAppFormAppUnitOfWork>()
          .To<AppFormAppUnitOfWork>()
          .InRequestScope();
}

I have a HomeController

public class HomeController : Controller
{
    private readonly IAppFormAppUnitOfWork _unitOfWork;
    public HomeController(
        IAppFormAppUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }
}

I get the error

No parameterless constructor defined for this object.

I also put a break point in RegisterServices(IKernel kernel) method and it gets hit.

What i am missing, doing wrong?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Catalin
  • 11,503
  • 19
  • 74
  • 147
  • 1
    possible duplicate of [ASP.NET MVC 4 + Ninject MVC 3 = No parameterless constructor defined for this object](http://stackoverflow.com/questions/12311479/asp-net-mvc-4-ninject-mvc-3-no-parameterless-constructor-defined-for-this-ob) – user247702 Sep 20 '13 at 09:58
  • Its such an annoying error. Basically though, it means Ninject wasn't registered properly and has not registered itself as the Controller Factory for MVC. – Simon Whitehead Sep 20 '13 at 10:04
  • Did you register your `NinjectControllerFactory` with `ControllerBuilder.Current.SetControllerFactory` or register your `NinjectRependencyResolver` with `DependencyResolver.SetResolver`? – Steven Sep 20 '13 at 10:11
  • @Steven: No, that was the problem. Before, on MVC3, it was all working by default. Not you need to manually register the Controller Factory – Catalin Sep 20 '13 at 10:16

1 Answers1

0

If you've registered your DependencyResolver.Current with Ninject, then, only one problem remains.

Ninject is getting an exception while is constructing your AppFormAppUnitOfWork to pass to your Constructor of your controller.

To find out what is that exception, you can write a few lines of code in some where such as begin request and try to resolve your object from DependencyResolver.Current.

Put that code in try catch, and then you will find the main source of a problem.

Good luck

Yaser Moradi
  • 3,267
  • 3
  • 24
  • 50