11

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;
Hashem Aboonajmi
  • 13,077
  • 8
  • 66
  • 75
  • 1
    The Web API dependency resolver is a completely different interface than the dependency resolver for MVC is. You will have to create another class for Web API. – Steven Jan 16 '14 at 10:13
  • I think there should be a way, a pattrn. some thing like an adapter. because they have some common method in their interface! – Hashem Aboonajmi Jan 16 '14 at 12:45
  • The solution is simple, create a class that implements both the `System.Web.Mvc.IDependencyResolver` and `System.Web.Http.IDependencyResolver` interface. – Steven Jan 16 '14 at 12:53
  • 1
    Related / duplicated: http://stackoverflow.com/questions/21520417/share-a-kernel-between-webapi-and-mvc/21528440#21528440 – cvbarros Feb 20 '14 at 14:49

2 Answers2

15

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

Charles380
  • 1,269
  • 8
  • 19
4

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?

bougiefever
  • 1,147
  • 10
  • 11
  • 2
    I believe it's NinjectDependencyResolver(kernel), not NinjectResolver, this blog post expands on your approach. http://nodogmablog.bryanhogan.net/2016/04/web-api-2-and-ninject-how-to-make-them-work-together/ – raterus Dec 18 '17 at 14:31