Asp.net mvc 5 控制器类中Ninject和null存储库中的AddBindings
在我的Asp.net mvc 5 控制器类中Ninject和null存储库中的AddBindings,asp.net-mvc-5,ninject.web.mvc,Asp.net Mvc 5,Ninject.web.mvc,在我的NinjectDependencyresolver中,我有: public NinjectDependencyResolver(IKernel kernelParam) { this.kernel = kernelParam; AddBindings(); } private void AddBindings() { kernel.Bind<IProductsRepository>().To<EFProductRepository>();
NinjectDependencyresolver
中,我有:
public NinjectDependencyResolver(IKernel kernelParam)
{
this.kernel = kernelParam;
AddBindings();
}
private void AddBindings()
{
kernel.Bind<IProductsRepository>().To<EFProductRepository>();
}
public class ProductController : Controller
{
private IProductsRepository repository;
public int PageSize = 4;
public ProductController()
{
}
public ProductController(IProductsRepository productRepository)
{
this.repository = productRepository;
}
问题是存储库
为空
另外,如果我将断点添加到addbinings()方法中,它在进入控制器之前不会被击中,controller
会被击中,但AddBindings()
不会。
那么这是否意味着这是我的Ninject
的问题
也:我在获得此错误后添加了产品控制器
的无参数构造函数
:
No parameterless constructor defined for this object
我不认为我需要那个构造函数,但是如果我删除它,我会得到那个错误。我会首先删除ProductController中没有参数的构造函数。这将迫使ninject将构造函数与IPProductsRepository一起使用
至于绑定部分,我们在NinjectWebCommon.cs文件中进行了绑定。这是我们的样本:
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(new VBNinjectModule());
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
BindWebSpecificServices(kernel);
GlobalConfiguration.Configuration.DependencyResolver = new VBNinjectDependencyResolver(kernel);
return kernel;
}
public static void BindWebSpecificServices(IKernel kernel)
{
kernel.Bind<IUserHelper>()
.To<UserHelper>()
.InRequestScope();
kernel.Bind<IRoleWebService>()
.To<RoleWebService>()
.InRequestScope();
}
公共静态类NinjectWebCommon
{
私有静态只读引导程序Bootstrapper=new Bootstrapper();
///
///启动应用程序
///
公共静态void Start()
{
RegisterModule(typeof(OnePerRequestHttpModule));
RegisterModule(typeof(NinjectHttpModule));
初始化(CreateKernel);
}
///
///停止应用程序。
///
公共静态无效停止()
{
Bootstrapper.ShutDown();
}
///
///创建将管理应用程序的内核。
///
///创建的内核。
私有静态IKernel CreateKernel()
{
var kernel=新的标准内核(新的VBNinjectModule());
kernel.Bind().ToMethod(ctx=>()=>newbootstrapper().kernel);
kernel.Bind().To();
BindWebSpecificServices(内核);
GlobalConfiguration.Configuration.DependencyResolver=新的VBNinjectDependencyResolver(内核);
返回内核;
}
公共静态void BindWebSpecificServices(IKernel内核)
{
kernel.Bind()
.至()
.InRequestScope();
kernel.Bind()
.至()
.InRequestScope();
}
也应该在Gloabal.ashx.cs
文件中调用它是的,无参数构造函数最初不在我的代码中,我不得不添加它,因为这个错误:“没有为这个对象定义无参数构造函数”我现在接受了它,现在得到了那个错误。“没有为这个对象定义无参数构造函数”这意味着它没有将ninject绑定到IPProductsRepository。请查看我的NinjectWebCommon并将其与您自己的进行比较。哦,好的,因此我将kernel.Bind().to();添加到CreateKernel()方法中,看起来您也在那里绑定它们。