3

Asp.NET MVCプロジェクトを開発しています。私のプロジェクトにはWeb APIもあります。私は、Visual Studio 3でASP.NET MVC5とWeb Api 2を使用しています。私はninjectを使用して依存性注入を行っています。私はWeb用のninjectがWeb Api 2で動作していないことを知っています。そこで、Web Api用のNinjectを使用しようとしました。下記の私のシナリオをご覧ください。 Ninject.Web.ApiでninjectをWeb Api 2で使用するとASP.NET MVC 5で動作しない

Iは

NinjectWebCommonに続い

enter image description here

、Iを加えパッケージマネージャをnuget使用

enter image description here

は、その後、私はNinject.Webをインストールパッケージマネージャをnuget使用してWeb 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クラスが1つの依存関係

[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>(); 
     }   
    } 
} 

を登録しているこれは私が自分のコードを実行すると、それは私にこのエラー

enter image description here

を与えているその後、私のコントローラ

public class HomeController : Controller 
    { 
     private ICategoryRepo categoryRepo; 

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

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

です

これは追加です

しかし、私がapiControllerにアクセスすると、動作しています。

は、ここに私のウェブAPIコントローラです

public class TestController : ApiController 
    { 
     private ICategoryRepo categoryRepo; 

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

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

だから私はそれは、Web APIのために働いたが、Webプロジェクトのために働いていないです出て見つけたもの。私は同じプロジェクトで両方を使用しています。

+0

とともにNinject.MVC5とセットアップMVCのためのDependencyResolverをインストールする必要があります。 –

答えて

6

あなたは、標準のMVCコントローラで使用したい場合はNuget経由 `Ninject.MVC5`をインストールWEBAPI

ため DependencyResolver
// 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(カーネル);カーネル作成メソッドで。それは両方のために働く。だからどうして? 2行目は必要ありませんか?私は本当に詳細にそれを知りたいです。あなたはそれについて説明することができますか? –

関連する問題