Using ninject dependecyResolver for both MVC and WebAPI

前端 未结 2 451
别跟我提以往
别跟我提以往 2020-12-31 16:19

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 d

2条回答
  •  既然无缘
    2020-12-31 16:44

    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

提交回复
热议问题