Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Error:
C#
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 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);
}
Posted
Updated 17-Jul-16 11:43am

1 solution

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??

C#
In ninjectWebCommon.Cs file:
private static void RegisterServices(IKernel kernel)
{
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
} 


changed to :

System.Web.Mvc.DependencyResolver.SetResolver(new ProjectNameSpaces.NinjectDependencyResolver(kernel));
 
Share this answer
 
Comments
Graeme_Grant 16-Sep-17 8:07am    
Please don't answer your own question with a solution and then accept it as a valid solution. This is see as REP farming and can get you banned if continued. Update your question instead.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900