我正在使用 ninject 3.0 和 asp.net mvc3。我在激活 ProductRepository 时收到错误消息
I'm using ninject 3.0 and asp.net mvc 3. I get the error rror activating IProductRepository
我是 MVC 的新手,所以我在看一个书本示例。但是当我 运行 程序时,它给我错误:激活 IProductRepository 时出错
没有可用的匹配绑定,并且该类型不可自绑定。
激活路径...
我已经阅读了很多文章和其他类似的错误,但其中 none 对我有所帮助。
如果一切都与书中的示例相同,为什么会发生这种情况?请提供一些帮助,我们将不胜感激。
public class NinjectControllerFactory : DefaultControllerFactory
{
private IKernel ninjectKernel;
public NinjectControllerFactory()
{
ninjectKernel = new StandardKernel();
}
protected override IController GetControllerInstance(RequestContext requestContext,
Type controllerType)
{
return controllerType == null
? null
: (IController)ninjectKernel.Get(controllerType);
}
private void AddBindings()
{
ninjectKernel.Bind<IProductRepository>().To<EFProductRepository>();
}
}
Global.asax.cs
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
}
IProductRepository.cs
public interface IProductRepository
{
IQueryable<Product> Products { get; }
}
EFProductRepository.cs
public class EFProductRepository : IProductRepository
{
private EFDbContext context = new EFDbContext();
public IQueryable<Product> Products
{
get { return context.Products; }
}
}
public class EFDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
}
ProductController.cs
public class ProductController : Controller
{
private IProductRepository repository;
public ProductController(IProductRepository repoParam)
{
repository = repoParam;
}
public ViewResult List()
{
return View(repository.Products);
}
}
更改您的方法以接受 IKernel
参数:
private void AddBindings(IKernel kernel)
{
kernel.Bind<IProductRepository>().To<EFProductRepository>();
}
然后正如 Mike 建议的那样,从您的 NinjectControllerFactory
构造方法中调用它:
public NinjectControllerFactory()
{
ninjectKernel = new StandardKernel();
AddBindings(ninjectKernal);
}
我是 MVC 的新手,所以我在看一个书本示例。但是当我 运行 程序时,它给我错误:激活 IProductRepository 时出错 没有可用的匹配绑定,并且该类型不可自绑定。 激活路径...
我已经阅读了很多文章和其他类似的错误,但其中 none 对我有所帮助。 如果一切都与书中的示例相同,为什么会发生这种情况?请提供一些帮助,我们将不胜感激。
public class NinjectControllerFactory : DefaultControllerFactory
{
private IKernel ninjectKernel;
public NinjectControllerFactory()
{
ninjectKernel = new StandardKernel();
}
protected override IController GetControllerInstance(RequestContext requestContext,
Type controllerType)
{
return controllerType == null
? null
: (IController)ninjectKernel.Get(controllerType);
}
private void AddBindings()
{
ninjectKernel.Bind<IProductRepository>().To<EFProductRepository>();
}
}
Global.asax.cs
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
}
IProductRepository.cs
public interface IProductRepository
{
IQueryable<Product> Products { get; }
}
EFProductRepository.cs
public class EFProductRepository : IProductRepository
{
private EFDbContext context = new EFDbContext();
public IQueryable<Product> Products
{
get { return context.Products; }
}
}
public class EFDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
}
ProductController.cs
public class ProductController : Controller
{
private IProductRepository repository;
public ProductController(IProductRepository repoParam)
{
repository = repoParam;
}
public ViewResult List()
{
return View(repository.Products);
}
}
更改您的方法以接受 IKernel
参数:
private void AddBindings(IKernel kernel)
{
kernel.Bind<IProductRepository>().To<EFProductRepository>();
}
然后正如 Mike 建议的那样,从您的 NinjectControllerFactory
构造方法中调用它:
public NinjectControllerFactory()
{
ninjectKernel = new StandardKernel();
AddBindings(ninjectKernal);
}