user182630
user182630

Reputation: 574

MVC2 with Ninject.Web.Mvc 2.2 and Ninject 2.2 runtime version 4.0 always asks for parameterless constructor for controllers

I have a previous project running Ninject 2.0 runtime version 2.0 and now I am using Ninject in a new project and using the new Ninject, ninject web.mvc version 2.2 for runtime version 4.0.

Every single time I get the error no parameterless constructor

Invalid Operation exception

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

What am I missing. All the bindings are registered.

Do I need to now define interfaces for Controllers as well such as HomeController as IHomeController as I have seen in some examples, Or do I get back to using the older version

Upvotes: 0

Views: 272

Answers (3)

user182630
user182630

Reputation: 574

I have seen this issue mentioned couple of times on forums where there is no direct answer, here is the solution to the above problem, i.e., working with latest ninject

  1. Download the latest Ninject from github.
  2. The ninject I got for MVC2 is named as Ninject.Web.Mvc2-2.2.0.0-release-net-4.0 (runtime version 4)
  3. Now during adding reference add Ninject.Web.Mvc.dll(check the version is same as above by right click properties in VS)
  4. Now Add Ninject.dll from the lib folder in same parent folder (check the version as above)
  5. Now Add CommonServiceLocator.NinjectAdapter.dll from the extensions folder in lib parent folder (check the version as above.)

The missing link in all these have been the commonserviceLocator.dll and the correct version should match. This should be tried if you are sure your bindings are correct as mine were and check to see if your project work with older version.

Thanks to everyone, and good luck :)

Upvotes: 1

StriplingWarrior
StriplingWarrior

Reputation: 156534

In addition to what Remo Gloor said, you might want to check that MVC is set up to use Ninject correctly. I was doing some things manually on an older version of the MVC plugin and ended up needing to just bite the bullet and make Global extend the NinjectHttpApplication class, which I had previously been avoiding.

The error you're getting is the error you would get if MVC tries using its built-in controller factory to produce controllers. So you may want to create a custom method binding on your controller class and put a breakpoint inside to make sure it's even being invoked.

You may also want to switch to version 2.3. You can pick up the latest builds of Ninject and all its extensions here.

Upvotes: 1

Remo Gloor
Remo Gloor

Reputation: 32725

There is one version that does not show activation exceptions properly but show this exception instead. Most likely the problem is a duplicated binding.

Upvotes: 1

Related Questions