Ninject and asp.net MVC4

11,031

Phil's article is about using Ninject with WebAPI. Is that what you mean? You don't need to do that for normal MVC injection. Also, you should be using the Ninject.MVC3 NuGet package with MVC4 (yes, I know it says MVC3, but it still works fine because NuGet sets up Assembly Version Redirects that make everything Just Work), this sets everything up and you don't need a controller factory. Just edit the NinjectWebCommon.cs file to add your bindings.

Share:
11,031

Related videos on Youtube

radbyx
Author by

radbyx

Backend developer at al dente. Former WebDeveloper at CapaSystems. I like to code in C#, MVC, JavaScript, Jquery, winforms, MSSQL, MySql ect. :) email: radbyx AT gmail DOT com

Updated on October 26, 2022

Comments

  • radbyx
    radbyx over 1 year

    I have a MVC3 application that I would like to port to MVC4. I am using Ninject for dependency injection. Using Nuget, I added "Ninject" to my project and created a controller factory as shown below

    public class NinjectControllerFactory : DefaultControllerFactory
    {
        private IKernel ninjectKernel;
    
        public NinjectControllerFactory()
        {
            ninjectKernel = new StandardKernel();
            AddBindings();
        }
    
        protected override IController GetControllerInstance(RequestContext  requestContext, Type controllerType)
        {
            return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType);
        }
    
        private void AddBindings()
        {
            //Add ninject bindings here
        }
    }
    

    This works fine for MVC3 but things have changed in MVC4. I did some digging and found this link that explains how to get ninject working for MVC4

    http://haacked.com/archive/2012/03/11/itrsquos-the-little-things-about-asp-net-mvc-4.aspx

    However, I am having trouble getting the code in the above link to compile. Specifically, The code that I am supposed to place in the Start() method of the web.common file gives me unresolved namespace errors

    GlobalConfiguration.Configuration.ServiceResolver
      .SetResolver(DependencyResolver.Current.ToServiceResolver());
    

    Both "ServiceResolver" and ".SetResolver" are unresolved. What references do I need to add to enable these? Also, if possible can you point me towards a tutorial showing me how to get ninject working in MVC4 without having to install the nuget package ninject.mvc3? I ask because I would prefer not to have any packages installed in my application that were written for MVC3 specifically to avoid things from breaking down the line if these nuget packages are updated.

    edit: I should have added that I am using Visual studio 2012 and .Net 4.5

  • Admin
    Admin over 11 years
    I dont need WebAPI. Where should I add my bindings in NinjectWebCommon.cs? In the "start" method? Also, do you see any potential for updates to the ninject.mvc3 package breaking my application? Thanks
  • Erik Funkenbusch
    Erik Funkenbusch over 11 years
    @curiouspanda - Updates can always break things, and Ninject has gone through a few iterations that have changed the "preferred" way of working with it, but in general code should remain compatible. If you don't use WebAPI then Phil's article does not apply. You register your mappings in the RegisterServices method of NinjectWebCommon.cs