在我的MVC3中使用Ninject时,我得到一个Ninject.ActivationException:激活通过Nuget安装的IRepository {Ranking}时出错

时间:2012-03-26 21:18:14

标签: ninject ninject.web.mvc

我通过nuget安装了Ninject,并在NinjectMVC3的RegisterServices方法(由nuget创建)中注册了我的绑定。请关注我的代码:

private static void RegisterServices(IKernel kernel)
{   
    kernel.Bind<IUnitOfWork>().To<UnitOfWork>();
    kernel.Bind<IRepository<Action>>().To<ActionRepository>();
    kernel.Bind<IRepository<ActionType>>().To<ActionTypeRepository>();
    kernel.Bind<IRepository<City>>().To<CityRepository>();
    kernel.Bind<IRepository<Country>>().To<CountryRepository>();
    kernel.Bind<IRepository<Goods>>().To<GoodsRepository>();
    kernel.Bind<IRepository<Media>>().To<MediaRepository>();
    kernel.Bind<IRepository<MediaType>>().To<MediaTypeRepository>();
    kernel.Bind<IRepository<Ranking>>().To<RankingRepository>();
    kernel.Bind<IRepository<Role>>().To<RoleRepository>();
    kernel.Bind<IRepository<Sponsor>>().To<SponsorRepository>();
    kernel.Bind<IRepository<State>>().To<StateRepository>();
    kernel.Bind<IRepository<UserAccountInfo>>().To<UserAccountInfoRepository>();
    kernel.Bind<IRepository<UserAction>>().To<UserActionRepository>();
    kernel.Bind<IRepository<UserDeservesGoods>>().To<UserDeservesGoodsRepository>();
    kernel.Bind<IRepository<UserGoods>>().To<UserGoodsRepository>();
    kernel.Bind<IRepository<User>>().To<UserRepository>();
    kernel.Bind<IUserService>().To<UserService>();
    kernel.Bind<IAccountService>().To<AccountService>();

    DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
}     

这是我的HomeController:

readonly IRepository<Ranking> repoRanking;
public HomeController(IRepository<Ranking> repoRanking)
{
    this.repoRanking = repoRanking;
}

当我执行HomeController时,我得到以下异常:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: Ninject.ActivationException: Error activating IRepository{Ranking}
No matching bindings are available, and the type is not self-bindable.
Activation path:
 2) Injection of dependency IRepository{Ranking} into parameter repoRanking of constructor of type HomeController
 1) Request for HomeController

Suggestions:
 1) Ensure that you have defined a binding for IRepository{Ranking}.
 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.

我做错了什么?

1 个答案:

答案 0 :(得分:2)

您是否创建了一个默认模块来设置绑定而不是寄存器服务方法?

 public class DefaultModule : NinjectModule {
    public override void Load() {
        Bind<IProductService>().To<ProductService>().InTransientScope();
    }
 }

然后在你的Global.asax文件中执行以下操作:

 protected override IKernel CreateKernel() {
  var kernel = new StandardKernel();

  kernel.Load(Assembly.GetExecutingAssembly());

 return kernel;
 }