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.
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 hereMock mock = new Mock();
mock.Setup(p => p.Products).Returns(new List
{
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().ToConstant(mock.Object);
}
In ninjectWebCommon.Cs file:
private static void RegisterServices(IKernel kernel)
{
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
}
IProductRepository Interface:
IEnumerable Products { get;}
Produt Controller:
private IProductRepository repository;
public ProductController(IProductRepository productRepository)
{
this.repository = productRepository;
}
// GET: Product
public ViewResult List()
{
return View(repository.Products);
}