views:

440

answers:

2

I am running a MVC 2 Preview and this is my first time trying to use Ninject2 MVC

There error that I am continently getting is: An error occurred when trying to create a controller of type 'MyMVC.Controllers.EventsController'. Make sure that the controller has a parameterless public constructor.

What I have in my Global.cs is this:

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

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

        routes.MapRoute(
            "Root",
            "",
            new { controller = "Home", action = "Index", id = "" }
        );
    }

    protected override void OnApplicationStarted()
    {
        RegisterRoutes(RouteTable.Routes);
        RegisterAllControllersIn(Assembly.GetExecutingAssembly());
    }

    protected override IKernel CreateKernel()
    {

        return new StandardKernel(new ServiceModule());
    }
}

internal class ServiceModule : NinjectModule
{
    public override void Load()
    {
        Bind<IEventService>().To<EventService>();

        Bind<IEventRepository>().To<EventRepository>();

    }
}

And this is what my Controller looks like.

public class EventsController : Controller
{
    private IEventService _eventService;
    //
    // GET: /Events/

    public EventsController(IEventService eventService)
    {
        _eventService = eventService;
    }
    public ActionResult Index(string name)
    {

        return View(_eventService.GetEvent(name));
    }

    public ActionResult UpcomingEvents()
    {
        return View(_eventService.GetUpcomingEvents().Take(3).ToList());
    }

}
A: 

At the risk of stating the obvious, you should try adding a parameterless constructor to your Events Controller.

public class EventsController : Controller
{
    private IEventService _eventService;
    //
    // Parameterless constructor, so NInject will work
    public EventsController() {}
    //
    // Regular constructor
    public EventsController(IEventService eventService)
    {
        _eventService = eventService;
    }
Robert Harvey
That was the first thing that I tried but then I get a null error for my service object. Also the samples found online do not have a empty constructor and they run just fine. Seems i'm missing something that is part of the Ninject config but I can't seem to pin point it.http://codeclimber.net.nz/archive/2009/08/14/how-to-use-ninject-2-with-asp.net-mvc.aspx
OneSmartGuy
+1  A: 

I've not used Ninject, but I would assume you need to implement your own IControllerFactory. Until they update it to MVC 2. Then utilize that instead of RegisterAllControllersIn(..):

ControllerBuilder.Current.SetControllerFactory(new MyNinjectControllerFactory());

EDIT: Again, i'm not all that familiar with Ninject but this might work as a simple factory:

public class MyNinjectControllerFactory : DefaultControllerFactory
{
            protected override IController GetControllerInstance(Type controllerType)
            {
               return [Container].GetInstance(controllerType) as Controller;               
            }
}
mxmissile
From what I have read this is done behind the ninject framework. However I am not familiar with this very much as well so I'll do some more digging around this.
OneSmartGuy
The NinjectHttpApplication that his application is inheriting from does all of this for him.
Charlino
@Charlino Yes, but I assume something is wrong with V2 of the MVC Framework and Ninject's HttpApplication implementation. Again, its just an assumption.
mxmissile
I am starting to think this is the case. I have looked everywhere and I don't seem to be missing anything. I will roll my own ControllerFactory and update with my outcome.
OneSmartGuy
did you ever get this to work?
Jitesh