3

我正在開發一個Asp.NET MVC項目。我的項目也有web api。我在Visual Studio 3中使用ASP.NET MVC5和Web Api 2.我正在使用ninject進行依賴注入。我知道ninject for web不適用於Web Api 2.所以我嘗試使用Ninject進行Web Api。請參閱下面的我的場景。使用Ninject和Ninject.Web.Api進行Web API Api 2在ASP.NET MVC中不起作用5

我使用的NuGet

enter image description here

然後我安裝Ninject.Web包管理

enter image description here

然後在NinjectWebCommon,我添加使用NuGet包管理器安裝用於網絡API 2包ninject這行在RegisterServices

private static void RegisterServices(IKernel kernel) 
     { 
      System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel); 
      kernel.Bind<ICategoryRepo>().To<CategoryRepo>(); 
     }  

這是我的全NinjectWebCommon類註冊一個依賴

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(PatheinFashionStore.Web.App_Start.NinjectWebCommon), "Start")] 
[assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(PatheinFashionStore.Web.App_Start.NinjectWebCommon), "Stop")] 

namespace PatheinFashionStore.Web.App_Start 
{ 
    using System; 
    using System.Web; 

    using Microsoft.Web.Infrastructure.DynamicModuleHelper; 

    using Ninject; 
    using Ninject.Web.Common; 
    using PatheinFashionStore.Domain.Abstract; 
    using PatheinFashionStore.Domain.Concrete; 

    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(); 
      try 
      { 
       kernel.Bind<Func<IKernel>>().ToMethod(ctx =>() => new Bootstrapper().Kernel); 
       kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>(); 
       System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel); 
       RegisterServices(kernel); 
       return kernel; 
      } 
      catch 
      { 
       kernel.Dispose(); 
       throw; 
      } 
     } 

     /// <summary> 
     /// Load your modules or register your services here! 
     /// </summary> 
     /// <param name="kernel">The kernel.</param> 
     private static void RegisterServices(IKernel kernel) 
     { 

      kernel.Bind<ICategoryRepo>().To<CategoryRepo>(); 
     }   
    } 
} 

這是我的控制器

public class HomeController : Controller 
    { 
     private ICategoryRepo categoryRepo; 

     public HomeController(ICategoryRepo categoryRepoParam) 
     { 
      this.categoryRepo = categoryRepoParam; 
     } 

     public ActionResult Index() 
     { 
      return View(); 
     } 
} 

後來,當我跑我的代碼,它給我這個錯誤

enter image description here

這是另外的

但是當我訪問apiController時,它正在工作。

這裏是我的網頁API控制器

public class TestController : ApiController 
    { 
     private ICategoryRepo categoryRepo; 

     public TestController(ICategoryRepo categoryRepoParam) 
     { 
      this.categoryRepo = categoryRepoParam; 
     } 

     public string Get() 
     { 
      this.categoryRepo.Create(); 
      return "OK"; 
     } 
    } 

所以,我發現了什麼是它正在爲網絡API,但不工作的web項目。我在同一個項目中使用了兩個。

+0

如果你想與一個標準的MVC控制器使用通過安裝'的NuGet Ninject.MVC5'。 –

回答

6

您需要與DependencyResolver一起安裝Ninject.MVC5和設置DependencyResolver爲MVC爲的WebAPI

// Web Api 
System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel); 

// MVC 
System.Web.Mvc.DependencyResolver.SetResolver(new Ninject.Web.Mvc.NinjectDependencyResolver(kernel)); 
+0

謝謝。有用。但我只添加這一行System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new Ninject.WebApi.DependencyResolver.NinjectDependencyResolver(kernel);在創建內核方法。它適用於兩者。那麼請問爲什麼?第二行不必要?我真的很想詳細瞭解它。請你解釋一下嗎? –