asp.net-mvc-4 - 使用Ninject的参数化 mvc 4 Web API 构造函数不工作

  显示原文与译文双语对照的内容
0 0

我按照本文的说明使用了 Ninject,用于 Web API 4控制器构造函数注入:

http://www.peterprovost.org/blog/2012/06/19/adding-ninject-to-web-api/

我所面临的问题是当我调用api方法时,我得到了错误说:"类型'CarController'没有默认构造函数"。

我甚至在 NinjectWebCommon.CreateKernel 上设置了断点来判断它是否被调用。 当应用程序运行时,它就会被调用。

我什么都没有?

另外,我从nuget安装了 Ninject.Web.Common 和Ninject来完成这个任务。 这是我的代码:

Web API 控制器:

public class CarController : ApiController
{
 private ICarService carService;
 public CarController(ICarService carService)
 {
 this.carService = carService;
 }
 [AcceptVerbs("GET")]
 public CarsResponse GetCars([FromUri] CarsRequest request)
 { 
 return this.carService.GetCars(request);
 }
}

App_Start中的

public class NinjectDependencyScope : IDependencyScope
{
 IResolutionRoot resolver;
 public NinjectDependencyScope(IResolutionRoot resolver)
 {
 this.resolver = resolver;
 }
 public object GetService(Type serviceType)
 {
 if (resolver == null)
 throw new ObjectDisposedException("this","This scope has been disposed");
 return resolver.TryGet(serviceType);
 }
 public System.Collections.Generic.IEnumerable<object> GetServices(Type serviceType)
 {
 if (resolver == null)
 throw new ObjectDisposedException("this","This scope has been disposed");
 return resolver.GetAll(serviceType);
 }
 public void Dispose()
 {
 IDisposable disposable = resolver as IDisposable;
 if (disposable!= null)
 disposable.Dispose();
 resolver = null;
 }
}
public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver
{
 IKernel kernel;
 public NinjectDependencyResolver(IKernel kernel)
 : base(kernel)
 {
 this.kernel = kernel;
 }
 public IDependencyScope BeginScope()
 {
 return new NinjectDependencyScope(kernel.BeginBlock());
 }
}

我的NinjectWebCommon的如下所示:

public static class NinjectWebCommon 
{
 private static readonly Bootstrapper bootstrapper = new Bootstrapper();
 public static void Start() 
 {
 DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
 DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
 bootstrapper.Initialize(CreateKernel);
 }
 public static void Stop()
 {
 bootstrapper.ShutDown();
 }
 private static IKernel CreateKernel()
 {
 var kernel = new StandardKernel();
 kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
 kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
 RegisterServices(kernel);
 GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
 return kernel;
 }
 private static void RegisterServices(IKernel kernel)
 {
 kernel.Bind<ICarService>().To<CarService>();
 } 
}
时间:原作者:6个回答

0 0

我也有同样的问题。 我通过以下方法找到了分辨率。 我忘了找到NinjectMVCDependencyResolver类的网页。

///<summary>
///Creates the kernel that will manage your application.
///</summary>
///<returns>The created kernel.</returns>
 private static IKernel CreateKernel()
 {
 var kernel = new StandardKernel();
 kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
 kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
 RegisterServices(kernel);
//Install our Ninject-based IDependencyResolver into the Web API config
 GlobalConfiguration.Configuration.DependencyResolver 
 = new NinjectDependencyResolver(kernel);
//Install into the MVC dependency resolver
 System.Web.Mvc.DependencyResolver.SetResolver(
 new NinjectMVCDependencyResolver(kernel));
 return kernel;
 }
public class NinjectMVCDependencyResolver : NinjectDependencyScope
, System.Web.Mvc.IDependencyResolver
{
 private IKernel kernel;
 public NinjectMVCDependencyResolver(IKernel kernel)
 : base(kernel)
 {
 this.kernel = kernel;
 }
 public IDependencyScope BeginScope()
 {
 return new NinjectDependencyScope(kernel.BeginBlock());
 }
}
原作者:
...