ASP.NET MVC 2 controller constructor with one parameter never gets called

184 views
Skip to first unread message

endy_tj

unread,
Nov 18, 2010, 1:53:26 AM11/18/10
to ninject
I have created a very simple ASP.NET MVC 2 application that can
reproduce my problem (only 14kb), in this address:
http://dl.dropbox.com/u/3888891/CobaNinject.zip

There is only one controller, HomeController, and one action, Index.
This controller has two constructors, one parameterless, the other has
one parameter. The data type of the parameter is an interface. I have
bound this interface to a concrete class in ninject kernel. The
problem is, this constructor with one parameter never gets called.

If I remove the parameterless constructor, it throws an error, "No
parameterless constructor defined for this object."

I have tried putting 'inject' attribute on the constructor with one
parameter, but no changes.

How do I setup ninject so it will call my constructor with one
parameter?

endy_tj

unread,
Nov 18, 2010, 4:02:03 AM11/18/10
to ninject
Extra info: I tried removing the parameterless constructor from
HomeController, and then resolving HomeController from the kernel
right after creating the kernel with this code:

Protected Overrides Function CreateKernel() As Ninject.IKernel
If NinjectKernel Is Nothing Then
NinjectKernel = New Ninject.StandardKernel
NinjectKernel.Bind(Of CobaNinject.IAda).To(Of
CobaNinject.IniAda)()
End If

' I tried this successfully, all dependencies resolved
Dim ctrl As IController = MvcApplication.NinjectKernel.Get(Of
CobaNinject.HomeController)()

Return NinjectKernel
End Function

Ninject resolved HomeController correctly without exception.

Maybe the default controller factory is called instead of
NinjectControllerFactory?

endy_tj

unread,
Nov 18, 2010, 5:43:49 AM11/18/10
to ninject
Reply all
Reply to author
Forward
0 new messages