得到“没有匹配绑定错误”我不知道我哪里做错了
问题说明
错误:
Error:
Error activating IProductRepository
No matching bindings are available, and the type is not self-bindable.
Activation path:
2) Injection of dependency IProductRepository into parameter productRepository of constructor of type ProductController
1) Request for ProductController
Suggestions:
1) Ensure that you have defined a binding for IProductRepository.
2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.
3) Ensure you have not accidentally created more than one kernel.
4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.
5) If you are using automatic module loading, ensure the search path and filters are correct.
我的尝试:
自定义解析器类中的My Binding Code:
私有内核;
public NinjectDependencyResolver(IKernel kernelParam)
{
this.kernel = kernelParam;
AddBindings();
}
公共对象GetService(类型serviceType)
{
返回kernel.TryGet(serviceType);
}
public IEnumerable< object> GetServices(类型serviceType)
{
返回kernel.GetAll(serviceType);
}
private void AddBindings()
{
//这里绑定
模拟< iproductrepository> mock = new Mock< iproductrepository>();
mock.Setup(p => p.Products).Returns(new List< product>
{
新产品{Name =HP Elite 3,价格= 800.50M},
新产品{Name =HP Spectra,价格= 1200.50M},
新产品{Name =HP Laser Printer,价格= 400.50M}
});
kernel.Bind< iproductrepository>()。ToConstant( mock.Object);
}
在ninjectWebCommon.Cs文件中:
private static void RegisterServices(IKernel内核)
{
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
}
IProductRepository接口:
IEnumerable< product>产品{get;}
产品控制器:
私有IProductRepository存储库;
public ProductController(IProductRepository productRepository)
{
this.repository = productRepository;
}
// GET:产品
public ViewResult List()
{
return View(repository.Products);
}
What I have tried:
My Binding Code in custome resolver class:
private IKernel kernel;
public NinjectDependencyResolver(IKernel kernelParam)
{
this.kernel = kernelParam;
AddBindings();
}
public object GetService(Type serviceType)
{
return kernel.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return kernel.GetAll(serviceType);
}
private void AddBindings()
{
// put bindings here
Mock<iproductrepository> mock = new Mock<iproductrepository>();
mock.Setup(p => p.Products).Returns(new List<product>
{
new Product {Name ="HP Elite 3", Price=800.50M },
new Product {Name ="HP Spectra", Price=1200.50M },
new Product {Name ="HP Laser Printer", Price=400.50M }
});
kernel.Bind<iproductrepository>().ToConstant(mock.Object);
}
In ninjectWebCommon.Cs file:
private static void RegisterServices(IKernel kernel)
{
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
}
IProductRepository Interface:
IEnumerable<product> Products { get;}
Produt Controller:
private IProductRepository repository;
public ProductController(IProductRepository productRepository)
{
this.repository = productRepository;
}
// GET: Product
public ViewResult List()
{
return View(repository.Products);
}
正确答案
我用完全限定的名称更改语句并且它有效。但是我不明白,即使我在名称空间的顶部使用了使用语句为什么它不起作用?
I change the statements with fully qualified name and it worked. But i did not understand even though i have used using statements on top with Namespaces Why it did not work??
In ninjectWebCommon.Cs file:
private static void RegisterServices(IKernel kernel)
{
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
}
更改为:
System.Web .Mvc.DependencyResolver.SetResolver(new ProjectNameSpaces.NinjectDependencyResolver(kernel));
changed to :
System.Web.Mvc.DependencyResolver.SetResolver(new ProjectNameSpaces.NinjectDependencyResolver(kernel));
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfgcgh
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
检查邮件是否发送成功
it1352 07-25 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
Android微调工具-删除当前选择
it1352 06-20 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20 -
希伯来语的空格句子标记化错误
it1352 06-22