将 ninject 与 Ninject.Web.Api 用于 Web Api 2 在 ASP.NET MVC 5 中不起作用
Using ninject with Ninject.Web.Api for Web Api 2 is not working in ASP.NET MVC 5
我正在开发一个 Asp.NET MVC 项目。我的项目也有 web api。我正在使用 ASP.NET MVC5 和 Web Api 2 以及 Visual Studio 3。我正在使用 ninject 进行依赖注入。我知道 ninject for web 不适用于 Web Api 2. 所以我尝试使用 Ninject for Web Api.
我安装了 ninject for web api 2 使用 nuget 包管理器的包
然后我使用 nuget 包管理器安装了 Ninject.Web
然后在NinjectWebCommon中,我在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 class 注册一个依赖项
[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";
}
}
所以我发现它适用于网络 api,但不适用于网络项目。我在同一个项目中使用两者。
您需要为 MVC 安装 Ninject.MVC5 并设置 DependencyResolver
以及为 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));
我正在开发一个 Asp.NET MVC 项目。我的项目也有 web api。我正在使用 ASP.NET MVC5 和 Web Api 2 以及 Visual Studio 3。我正在使用 ninject 进行依赖注入。我知道 ninject for web 不适用于 Web Api 2. 所以我尝试使用 Ninject for Web Api.
我安装了 ninject for web api 2 使用 nuget 包管理器的包
然后我使用 nuget 包管理器安装了 Ninject.Web
然后在NinjectWebCommon中,我在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 class 注册一个依赖项
[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";
}
}
所以我发现它适用于网络 api,但不适用于网络项目。我在同一个项目中使用两者。
您需要为 MVC 安装 Ninject.MVC5 并设置 DependencyResolver
以及为 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));