Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ninject dependecyResolver for both MVC and WebAPI

I have created and MVC 4 web application and decided to use web api in this app. I'm using ninject dependency resolver for MVC web app. and now I want to use this ninject dependency resolver for web api. but the problem raise here mvc IDependencyResolver namespace is: using System.Web.Mvc and web api IDependencyResolver is using System.Web.Http.Dependencies

so how can I solve this issue?

finally I want something like this:

// Use the container and the NinjectDependencyResolver as
        // application's resolver
        var resolver = new NinjectDependencyResolver(container);

        //Register Resolver for MVC
        DependencyResolver.SetResolver(resolver);

        //Register Resolver for Web Api
        GlobalConfiguration.Configuration.DependencyResolver = resolver;
like image 618
Hashem Aboonajmi Avatar asked Jan 16 '14 09:01

Hashem Aboonajmi


People also ask

How to use ninject container in MVC and web API?

Usage is very simple. To use the same container in MVC and Web API project we just create Ninject container in Global.asax.cs and pass same Ninject resolver to MVC and Web API.

How to resolve all dependencies in MVC with ninject?

Because the NinjectDependencyResolver class will use the TryGet () method to resolve all dependencies. That's it; you are now done, your MVC application will hit the configured Ninject container instance to resolve all dependencies needed during controller activation.

How do I implement WebAPI with ninject?

To get started with this implementation create a new WebAPI project in Visual Studio. Next, install the necessary packages from NuGet to work with NInject. You can install the Ninject.Web.WebApi.WebHost package via the NuGet Package Manager. This would in turn install the following two packages for you.

Why dependency resolver is not available in MVC 5?

Reason for this is because ASP.NET MVC 5 uses interface System.Web.Mvc.IDependencyResolver for implementing dependency resolver and ASP.NET Web API uses System.Web.Http.Dependencies.IDependencyResolver interface which has the same name but resides in different namespace. These two interfaces are different, even they have a same name.


2 Answers

There is a way to share same container between MVC and ASP.NET Web API. You just need to implement both interfaces.

public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver, System.Web.Mvc.IDependencyResolver
   {
       private readonly IKernel kernel;
 
       public NinjectDependencyResolver(IKernel kernel)
           : base(kernel)
       {
           this.kernel = kernel;
       }
 
       public IDependencyScope BeginScope()
       {
           return new NinjectDependencyScope(this.kernel.BeginBlock());
       }
   }

Check this article for solution: Simple Way to share Dependency Resolvers between MVC and Web API

like image 157
Nenad Hummingbird Avatar answered Sep 28 '22 03:09

Nenad Hummingbird


There is a NuGet package that does this. Add the NInject, NInject.Web.Common, NInject.MVCx and WebApiContrib.IoC.Ninject NuGet packages to your project. A NInjectWebCommon class should have been created in the App_Start folder. Add your binding for your dependencies to the RegisterServices method. In the CreateKernel method after the RegisterServices(kernel) call, add the following line:

GlobalConfiguration.Configuration.DependencyResolver = new NinjectResolver(kernel);

This will let you create the Ninject dependency resolver without having to create your own override class. Easy, right?

like image 43
bougiefever Avatar answered Sep 28 '22 05:09

bougiefever