• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

得到“没有匹配绑定错误”我不知道我哪里做错了

用户头像
it1352
帮助1

问题说明

错误:

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);
}

正确答案

#1
我用完全限定的名称更改语句并且它有效。但是我不明白,即使我在名称空间的顶部使用了使用语句为什么它不起作用?



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
系列文章
更多 icon
同类精品
更多 icon
继续加载