views:

930

answers:

4

Hello,

i am using:-

  1. asp.net mvc rc 2
  2. Ninject and ninject asp.net mvc extension (http://github.com/enkari/ninject.web.mvc)

i keep getting the 'No parameterless constructor defined for this object.' for my AccountController. The AccountController is injected with Services. The bindings for these services are defined in the ServiceModule.

Find below the code for my MvcApplication in Global.asax.cs.

public class MvcApplication : NinjectHttpApplication // System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Account", action = "Login", id = "" }  // Parameter defaults
        );

    }

    protected override void OnApplicationStarted()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterRoutes(RouteTable.Routes);

        RegisterAllControllersIn(Assembly.GetExecutingAssembly());
    }

    protected override IKernel CreateKernel()
    {
        return new StandardKernel(new INinjectModule[] { 
            new ServiceModule(),
        });
    }
}
A: 

In Ninject, you're not supposed to override the Application_Start method. If you need things done that aren't automatically (like registering areas - I don't know off the top of my head if Ninject does that for you as well), be sure to call base.OnApplicationStarted() to make all the Ninject-specific stuff run properly.

protected override void OnApplicationStarted()
{
    base.OnApplicationStarted();

    // Kick some butt here
}
Tomas Lycken
From what i see in the 'NinjectHttpApplication.cs' file, the function is empty. i agree that i should do it the way you said(because it is virtual), but that does not solve my problem.
Jitesh
A: 

The code looks correct except for the comma after 'new ServiceModule()'.

It should be in Global.asax.cs, not Global.aspx.cs.

ScottS
Edited the file name in the question. It was a typing mistake. The comma does not matter. it is typing syntax.
Jitesh
+7  A: 

Rebuilding the Ninject.Web.Mvc against the ASP.Net MVC 2 dlls fixed the issue. The problem is with the NinjectControllerFactory class. The signature of the method to get the controller instance has changed in MVC 2.

IController GetControllerInstance(Type controllerType)

To

IController GetControllerInstance(
        RequestContext requestContext, Type controllerType)

Make the necessary changes and rebuild the Ninject MVC extension and all works fine. Thanks to @Charlino for the suggestion.

Jitesh
Good to hear. Gutted I didn't make it an answer and get the points! ;-)
Charlino
A: 

For what ever reason, I have found that if your global.asax.cs inherits from NinjectHttpApplication OnApplicationStarted() does not get called. Change your OnApplicationStarted() to override Init(), and it should work.

See below:

public class MvcApplication : NinjectHttpApplication // System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Account", action = "Login", id = "" }  // Parameter defaults
        );

    }

    public override void Init()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterRoutes(RouteTable.Routes);

        RegisterAllControllersIn(Assembly.GetExecutingAssembly());
    }

    protected override IKernel CreateKernel()
    {
        return new StandardKernel(new INinjectModule[] { 
            new ServiceModule(),
        });
    }
}
Nathan Lee