No parameterless constructor defined for this object.

1,415 views
Skip to first unread message

Fred Chateau

unread,
Jan 25, 2013, 8:38:22 PM1/25/13
to nin...@googlegroups.com
I am using Ninject 3 with MVC-4. I have the traditional recommended setup in Global.asax.cs (no Nuget) and use standard constructor injection.

        protected override IKernel CreateKernel()
        {
            return Container;
        }

        private static IKernel Container
        {
            get
            {
                IKernel kernel = new StandardKernel();
                kernel.Load(new WebModule(), new ModelsModule());
                return kernel;
            }
        }

Whenever I try to request a controller action method I receive the "No parameterless constructor defined for this object" error.

How can I determine why this is happening?

Fred Chateau


Fred Chateau

unread,
Jan 27, 2013, 11:11:49 PM1/27/13
to nin...@googlegroups.com

Never mind. I got it working. Apparently this is a bug in Visual Studio 2012.

Here's the workaround to it:

    public class NinjectControllerFactory : DefaultControllerFactory
    {
        private readonly IKernel _ninjectKernel;

        public NinjectControllerFactory(IKernel kernel)
        {
            _ninjectKernel = kernel;
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            return (controllerType == null) ? null : (IController)_ninjectKernel.Get(controllerType);
        }
    }

And add this to Global.asax OnApplicationStarted() :

    ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory(CreateKernel()));

Bartelink, Ruben

unread,
Jan 28, 2013, 4:03:01 AM1/28/13
to nin...@googlegroups.com
The 'traditional recommended' setup you are referring to appears to be
regarding ASP.NET [w/o MVC] (but you didnt mention if you're deriving
from anything or putting anything in your web.config) (yes, the
override suggests you are...)

> Never mind. I got it working. Apparently this is a bug in Visual Studio
> 2012.

Not sure what you're referring to. Best to give a citation less
someone not privy to that insight come across this.

MVC's default Controller Factory will give rise to the exception you saw.

Using the Ninject.MVC3 package adds a Controller Factory and does lots
of other important subtle things regarding sequencing of requests and
teardowns. See https://github.com/ninject/ninject.web.mvc/wiki/MVC3

If you're absolutely sure none of the concerns Ninject.MVC3 addresses
are relevant to you then yes, the simplest thing that could possibly
work is fine. Failing that, the recommended approach is Ninject.MVC3
unless you have really good reasons to ignore that.

--Ruben
> --
> You received this message because you are subscribed to the Google Groups
> "ninject" group.
> To post to this group, send email to nin...@googlegroups.com.
> To unsubscribe from this group, send email to
> ninject+u...@googlegroups.com.
> Visit this group at http://groups.google.com/group/ninject?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
--Ruben
Ruben Bartelink / +353 86 885 2399

Jarrett Meyer

unread,
Jan 28, 2013, 6:57:09 AM1/28/13
to nin...@googlegroups.com
+1 Ruben,

Use Ninject.MVC in your MVC project unless you have a really compelling reason not to. It sets up the controller factory and more for you. It allows injection into filters and model validators, too. 

Jarrett

--
Jarrett Meyer
Email: jarret...@gmail.com
Web: JarrettMeyer.com
Twitter: @jarrettmeyer


To unsubscribe from this group and stop receiving emails from it, send an email to ninject+u...@googlegroups.com.

To post to this group, send email to nin...@googlegroups.com.

Fred Chateau

unread,
Jan 28, 2013, 2:30:34 PM1/28/13
to nin...@googlegroups.com
Thank you for your reply.

I didn't say I wasn't using Ninject.MVC.  Ruben said that.  And, by the way, this is not my first time using Ninject.MVC

 These are the relevant portions of my Global.asax:

    public class MvcApplication : NinjectHttpApplication
    {
        protected override void OnApplicationStarted()
        {
            base.OnApplicationStarted();

            ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory(CreateKernel()));

        }

        protected override IKernel CreateKernel()
        {
            return Container;
        }

        private static IKernel Container
        {
            get
            {
                IKernel kernel = new StandardKernel();
                kernel.Load(new ServiceModule(), new RepositoryModule());
                return kernel;
            }
        }

Without the custom NinjectControllerFactory, all I get is the dreaded "No parameterless constructor defined for this object" error.

I read in StackOverflow that this is a known occurrence when using Visual Studio 2012. Someone posted the custom NinjectControllerFactory code there, so I tried it, and it worked. The error stopped.

If you know a better alternative, please post it.


Fred Chateau


 


On Friday, January 25, 2013 7:38:22 PM UTC-6, Fred Chateau wrote:

Fred Chateau

unread,
Jan 28, 2013, 2:41:28 PM1/28/13
to nin...@googlegroups.com
By the way, I'm not adding anything to Web.config, so perhaps that is the problem. What should I add there?

Fred

Jarrett Meyer

unread,
Jan 28, 2013, 2:43:42 PM1/28/13
to nin...@googlegroups.com
Do you have an App_Start folder? It would have been added by the Ninject.Web.Common library.

--
Jarrett Meyer
Email: jarret...@gmail.com
Web: JarrettMeyer.com
Twitter: @jarrettmeyer


Fred Chateau

unread,
Jan 28, 2013, 3:20:22 PM1/28/13
to nin...@googlegroups.com
I am not using Nuget. There is no Web Activator. That is why I posted the Global.asax code for you to review.

Basically, I copied my Global.asax code from one of my previous MVC3 applications that is working properly.

Fred Chateau

unread,
Jan 28, 2013, 3:53:02 PM1/28/13
to nin...@googlegroups.com
Well, never mind...

I wasn't using the MVC v3 to v4 redirects in web.config. Never needed them before. Odd thing was I never got an error about wrong version.

Serves me right for checking with StackOverflow before checking here first, :)  but I had forgotten about this group.

Sorry to bother everyone, and thanks for trying to help.


Fred Chateau

Fred Chateau

unread,
Jan 30, 2013, 7:08:45 PM1/30/13
to nin...@googlegroups.com

I don't like using the MVC v3 to v4 redirects, so I re-compiled the Ninject.Extensions.MVC project while referencing the MVC-4 binary, and it appears to be working well.

If anyone wants a copy of the resulting binaries or source solution (Visual Studio 2012) email me at: FredDotChateauAtHospicelinkDotCom and I'll send them to you.

Remo Gloor

unread,
Jan 31, 2013, 3:49:23 AM1/31/13
to nin...@googlegroups.com

Hi Fred

 

If you have the time I’d highly appreciate if you can fork the github repository, add a MVC4 project and send a pull request.

 

Remo

To unsubscribe from this group and stop receiving emails from it, send an email to ninject+u...@googlegroups.com

.


To post to this group, send email to

Reply all
Reply to author
Forward
0 new messages