I'm trying to use the new ASP.Net MVC 4 Web API project template with Ninject but have hit a wall on the following error:

Method 'GetFilters' in type 'Ninject.Web.WebApi.Filter.DefaultFilterProvider' from assembly 'Ninject.Web.WebApi, Version=3.0.0.0, Culture=neutral, PublicKeyToken=c7192dc5380945e7' does not have an implementation.

I am creating a brand new project in Visual Studio 2010 using the ASP.Net MVC 4 -> Web API template and I am using the latest Ninject NuGet packages:

  • Ninject 3.0.1.10
  • Ninject.Web.Common 3.0.0.7
  • Ninject.Web.WebApi 3.0.0.2

I have attempted the solution presented in this question however I've not had any luck - if I remove the reference to Ninject.Web.WebApi then MVC never engages Ninject. I also notice they mention Ninject.MVC3 however I am using the new Ninject.WebApi plugin.

I am using the default binding code in NinjectWebCommon.cs that is created during the NuGet install and attempting to register one simple service in RegisterServices()

[assembly: WebActivator.PreApplicationStartMethod(typeof(mkts.web.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(mkts.web.App_Start.NinjectWebCommon), "Stop")]

namespace mkts.web.App_Start
{
    using System;
    using System.Web;

    using Microsoft.Web.Infrastructure.DynamicModuleHelper;

    using Ninject;
    using Ninject.Web.Common;
    using mkts.service;

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

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start() 
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            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();
            kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

            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)
        {
            //Test binding
            kernel.Bind<IStudentService>().To<StudentService>();
        }        
    }
}

My controller:

namespace mkts.web.Controllers
{
    public class HomeController : Controller
    {
        private readonly IStudentService studentService;

        public HomeController(IStudentService studentService)
        {
            this.studentService = studentService;
        }


        public ActionResult Index()
        {
            return View();
        }
    }
}

Many thanks in advance for any help with this.

share|improve this question
What a conincidence I am also using IStudentService, StudentService names :) – TheVillageIdiot Jan 30 at 4:20

1 Answer

up vote 26 down vote accepted

Ninject.WebApi was written against the Beta and is deprecated now.

Remove that, install Ninject.MVC3.

Now you will need a homebrewed IDependencyResolver and IDependencyScope. I posted a walkthrough here - http://www.strathweb.com/2012/05/using-ninject-with-the-latest-asp-net-web-api-source/

share|improve this answer
That's it! I scrapped Ninject.Web.WebApi and replaced with Ninject.MVC3 and then added your custom DependencyResolver class and now everything is working. I somehow missed the fact in my travels that Ninject.WebApi was deprecated and the naming convention threw me as I figured a plugin named Ninject.WebApi would support the MVC 4 WebApi projects - they should really update the nuget page with the fact that it is no longer supported. Anyway, many thanks for your answer, this was really frustrating. – mmacneil007 Jun 26 '12 at 17:46
4  
Is there any hope of the Ninject team updating the NuGet package with an API compatible build? – Ciel Oct 12 '12 at 17:56
I would vote this up N times if I could! – Kaizen Dec 12 '12 at 17:00
Following the above link and creating the two classes NinjectScope and NinjectResolver did solve the problem. I did not need Ninject.MVC3 – muruge Dec 12 '12 at 20:03
This is a great contribution Filip; Really useful, Thanks. – Neil Thompson Dec 18 '12 at 14:58
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.