3

我通过 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.

我做错了什么?

4

1 回答 1

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;
 }
于 2012-03-26T21:26:49.767 回答