Issue using ASP.Net MVC 4 Web API with Ninject.Web.WebApi
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/
Related videos on Youtube
mmacneil007
Updated on June 03, 2022Comments
-
mmacneil007 almost 2 years
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.
-
TheVillageIdiot over 11 yearsWhat a conincidence I am also using
IStudentService
,StudentService
names :)
-
mmacneil007 almost 12 yearsThat'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.
-
Ciel over 11 yearsIs there any hope of the Ninject team updating the NuGet package with an API compatible build?
-
muruge over 11 yearsFollowing the above link and creating the two classes NinjectScope and NinjectResolver did solve the problem. I did not need Ninject.MVC3
-
Neil Thompson over 11 yearsThis is a great contribution Filip; Really useful, Thanks.
-
chobo2 over 11 yearsHave they not released a newer version of this stuff or do you still have to do a home brewed?