0

I upgraded from ninject 2.0 to 2.2 and nothing works anymore.

When I use nuget it makes this

[assembly: WebActivator.PreApplicationStartMethod(typeof(MvcApplication3.App_Start.NinjectMVC3), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(MvcApplication3.App_Start.NinjectMVC3), "Stop")]

namespace MvcApplication3.App_Start
{
    using System.Reflection;
    using Microsoft.Web.Infrastructure.DynamicModuleHelper;
    using Ninject;
    using Ninject.Web.Mvc;

    public static class NinjectMVC3 
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start() 
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestModule));
            DynamicModuleUtility.RegisterModule(typeof(HttpApplicationInitializationModule));
            bootstrapper.Initialize(CreateKernel);
        }

        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }

        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            RegisterServices(kernel);
            return kernel;
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {

        }        
    }
}


I use this






     /// <summary>
            /// Application_Start
            /// </summary>
            protected void Application_Start()
            {

                // Hook our DI stuff when application starts
                IKernel kernel = SetupDependencyInjection();

            }


            public IKernel SetupDependencyInjection()
            {
                IKernel kernel = CreateKernel();
                // Tell ASP.NET MVC 3 to use our Ninject DI Container
                DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));

                return kernel;
            }

            protected IKernel CreateKernel()
            {
                var modules = new INinjectModule[]
                                  {
                                     new NhibernateModule(),
                                     new ServiceModule(),
                                     new RepoModule()
                                  };


  public class NinjectDependencyResolver : IDependencyResolver
    {
        private readonly IResolutionRoot resolutionRoot;

        public NinjectDependencyResolver(IResolutionRoot kernel)
        {
            resolutionRoot = kernel;
        }

        public object GetService(Type serviceType)
        {
            return resolutionRoot.TryGet(serviceType);
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return resolutionRoot.GetAll(serviceType);
        }
    }

So when I try to use my way(what worked before the changes) when I load it up now I get some no parameterless controller.

When I use their I get

Error occured: Error activating SomeController
More than one matching bindings are available.
Activation path:
  1) Request for SomeController

Suggestions:
  1) Ensure that you have defined a binding for SomeController only once.
Remo Gloor
  • 32,665
  • 4
  • 68
  • 98
chobo2
  • 83,322
  • 195
  • 530
  • 832

3 Answers3

1

Move your module array into the

var modules = new INinjectModule[]
                                  {
                                     new NhibernateModule(),
                                     new ServiceModule(),
                                     new RepoModule()
                                  };

into the RegisterServices and add

kernel.Load(modules);
Christian Specht
  • 35,843
  • 15
  • 128
  • 182
Eric Smith
  • 79
  • 1
  • 6
  • I will try that again. I did something similar but I am trying to figure out what the difference between what I had before and what gets generated with the ninject mvc 3 plugin. – chobo2 Jun 17 '11 at 05:28
1

I hope you know that there is a documentation at https://github.com/ninject/ninject.web.mvc/wiki/Setting-up-an-MVC3-application where this question is explained.

Remo Gloor
  • 32,665
  • 4
  • 68
  • 98
0

Those are two different approaches into configuring the kernel. The approach you were using requires modifying Global.asax. The NuGet package uses this new feature of ASP.NET 4 which allows dynamic modules to be registered at application startup. Because the authors of the NuGet package didn't want to mess up with Global.asax as there might be some other existing code, they preferred to use this separate file for configuring the kernel.

The two approaches are not compatible and you should never use them both in the same application. The new version also already contains a NinjectDependencyResolver so you no longer need to write or set any custom DependencyResolver.SetResolver.

All you need to do is use the RegisterServices static method in the bootstrapper class to configure your kernel:

private static void RegisterServices(IKernel kernel)
{
    kernel.Bind<ISomeControllerDependency>().To<SomeConcreteImpl>();
}        

And if you had some NInject modules that you wanted to load:

private static void RegisterServices(IKernel kernel)
{
    kernel.Load(
        new NhibernateModule(),
        new ServiceModule(),
        new RepoModule()
    );
}        

That's it. Don't forget to remove any NInject trace from your Global.asax to avoid any conflicts.

I guess the reason your code didn't work with the first approach is because you didn't load the modules in the RegisterServices method.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • @Darin Dimitrov - Actually I did load the modules in. I did it a bit differently. I just did not show it there as I reverted the code back. I may move to the one then that is provided. However I am just trying to understand why what I had does not all of a sudden work. I also trying to figure out why when I did move to the new soultion it still did not work either. From what I found http://stackoverflow.com/questions/2423862/error-more-than-one-matching-bindings-are-available-when-using-ninject-web-mvc is that you have to bind the controllers to it's self. I never had to do that before. – chobo2 Jun 17 '11 at 15:53
  • So to me it seems I am sort of going backwards. A new version has come out and now I have to bind controllers to themselves(if this does fix my problem I have not tried it yet) when I never had to do this before. – chobo2 Jun 17 '11 at 15:54
  • @chobo2, binding *controllers to themselves*? Could you explain what this means? I am not sure I understand you. As long as you have controller that takes some interface as a constructor parameter and this interface is registered withing the kernel with a proper implementation you don't need to bind any controllers to anything. – Darin Dimitrov Jun 17 '11 at 16:05
  • @Darin Dimitrov - Read the accepted answer from the link I posted. I am getting the same error has him. When I walked through the debugger I had something that needed to be bind and it bound that correctly. I then went to try load up a controller and it all of sudden said it could not find a constructor with no parameters. Of course pre upgrading it all worked. – chobo2 Jun 17 '11 at 16:19
  • @chobo2, can you send me a sample project illustrating the problem? I think that would be the easiest for me understanding your scenario, what are you trying to do and what doesn't work. – Darin Dimitrov Jun 17 '11 at 16:28
  • @Darin Dimitrov - It seems I did for some reason have a duplicate in my repo. Why it worked with the old version ninject and not the new version I am unsure. So I will be testing it and get back to you soon. – chobo2 Jun 17 '11 at 17:51
  • @chobo2 Ninject 2.0 took one binding if several matched. But because it was not determined which one due to an unstable sort algorithm it is now considered as invalid configuration. So if one is expected it must be unique now. – Remo Gloor Jun 17 '11 at 23:06