Issue using ASP.Net MVC 4 Web API with Ninject.Web.WebApi
Asked Answered
T

1

26

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.

Transmute answered 26/6, 2012 at 12:55 Comment(1)
What a conincidence I am also using IStudentService, StudentService names :)Mede
M
62

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/

Miquelon answered 26/6, 2012 at 17:13 Comment(5)
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.Transmute
Is there any hope of the Ninject team updating the NuGet package with an API compatible build?Terpsichorean
Following the above link and creating the two classes NinjectScope and NinjectResolver did solve the problem. I did not need Ninject.MVC3Vinaigrette
This is a great contribution Filip; Really useful, Thanks.Registrant
Have they not released a newer version of this stuff or do you still have to do a home brewed?Leanto

© 2022 - 2024 — McMap. All rights reserved.