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に続い、Iを加えパッケージマネージャをnuget使用
は、その後、私はNinject.Webをインストールパッケージマネージャをnuget使用してWeb API 2パッケージのninjectをインストールこの行はRegisterServicesprivate 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>();
}
}
}
を登録しているこれは私が自分のコードを実行すると、それは私にこのエラー
を与えているその後、私のコントローラ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プロジェクトのために働いていないです出て見つけたもの。私は同じプロジェクトで両方を使用しています。
とともにNinject.MVC5とセットアップMVCのための
DependencyResolver
をインストールする必要があります。 –