2

This is an MVC application where the controllers need a DataContextCreator and a CustomerID in the constructor. My ControllerFactory looks like:

public class NinjectControllerFactory : DefaultControllerFactory
    {
        private IKernel ninjectKernel;

        public NinjectControllerFactory()
        {
            ninjectKernel = new StandardKernel();
            AddBindings();
        }


        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            if (controllerType == null)
            {
                return null;
            }
            else
            {
                string customerID =  requestContext.HttpContext.Session["CustomerID"].ToString();
                return (IController)ninjectKernel.Get(controllerType, new IParameter[]{new Parameter("CustomerID", customerID, true)});
            }
        }

        private void AddBindings()
        {
            ninjectKernel.Bind<IDataContextCreator>().To<DefaultDataContextCreator>();
        }
    }

I get the following error when navigating to the Page i.e. triggering the creation of the Controller:

 Ninject.ActivationException: Error activating int
No matching bindings are available, and the type is not self-bindable.
Activation path:
 2) Injection of dependency int into parameter CustomerID of constructor of type MyController
 1) Request for MyController

All of the above is using MVC3 .Net 4 on Win 7. Thanks for your help.

O.O.
  • 1,973
  • 6
  • 28
  • 40

1 Answers1

5

why did you write a custom controller factory? This is not common when working with the Ninject.MVC3 NuGet package. A more common technique is to use the custom dependency provider that is automatically registered for you when you install this NuGet.

So here are the steps:

  1. Get rid of your custom controller factory
  2. Install the Ninject.MVC3 NuGet package.
  3. Inside the ~/App_Start/NinjectWebCommon.cs file configure your kernel

    private static void RegisterServices(IKernel kernel)
    {
        kernel
            .Bind<IDataContextCreator>()
            .To<DefaultDataContextCreator>();
        kernel
            .Bind<MyController>()
            .ToSelf()
            .WithConstructorArgument("customerID", ctx => HttpContext.Current.Session["CustomerID"]);
    }        
    
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thank you. I did perform your steps, but I cannot hit any break point in `NinjectWebCommon` e.g. in the 'Start()` Method. Just to be sure, I deleted `NinjectControllerFactory`, installed `Ninject.MVC3` and got the `App_Start/NinjectWebCommon.cs` file. I then edited the `RegisterServices()` method like above - but I get the same error. I then figured out that `NinjectWebCommon` is not being called anywhere?? – O.O. Oct 15 '12 at 22:31
  • The Start method should be called when you run the application for the first time. – Darin Dimitrov Oct 16 '12 at 05:45
  • Where do I call the `Start()` method?? I first attempted to call it in `Application_Start()`. This worked, but I got the exception `InvalidOperationException` crossed a native/managed boundary This method can only be called during the application's pre-start initialization stage. I next realized that I might need to have an `NinjectHttpApplication`. So I did the setup as shown in [link](https://github.com/ninject/ninject.web.mvc/wiki/Setting-up-an-MVC3-application). Here I get the exception: `System.InvalidOperationException`: Sequence contains no elements – O.O. Oct 16 '12 at 13:58
  • I just attempted the sample MVC3 application given [here] (http://github.com/ninject/ninject.web.mvc/zipball/master) and it does not work. I don't get an exception but I get **The resource cannot be found.** error – O.O. Oct 16 '12 at 14:32
  • You should not manually call the `Start` method. It is automatically called when the application starts. Here are some steps you may follow: http://stackoverflow.com/a/11477061/29407 – Darin Dimitrov Oct 16 '12 at 16:29
  • Thank you **Darin** for all your help. I think **Ninject** is way more trouble than it is worth. I ended up uninstalling all **Ninject** – even the parts that worked before. For now I have decided to use my custom `ControllerFactory` and create the Controllers manually as in http://stackoverflow.com/a/12924017/1356847 – O.O. Oct 16 '12 at 22:00