Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i get ninject 2.0 working with asp.net mvc 2?

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(),
        });
    }
}
like image 316
Jitesh Patil Avatar asked Feb 11 '10 20:02

Jitesh Patil


2 Answers

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.

like image 81
Jitesh Patil Avatar answered Oct 18 '22 20:10

Jitesh Patil


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(),
        });
    }
}
like image 32
Nathan Lee Avatar answered Oct 18 '22 19:10

Nathan Lee