Search code examples
asp.net-mvcasp.net-mvc-3ninjectninject-2

Whats the difference between using these 2 methods?


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.

Solution

  • 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.